Sub topic 2
Adding `flutter_localizations` and `intl` to a Flutter Project
This chapter assumes that you know basic Dart null safety, can build a Flutter app with MaterialApp, and can edit pubspec.yaml. You should also know how to run flutter pub get.
Flutter localization has two parts:
- Internationalization prepares an app to support multiple languages and regional formats.
- Localization supplies the translated text and locale-specific formats for one language or region.
The flutter_localizations package connects Flutter widgets to localized material and Cupertino text. The intl package provides locale-aware formatting, such as dates, numbers, and plurals.
Add the dependencies
Open the project's pubspec.yaml file. Add the packages under the existing dependencies section:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: any
flutter:
generate: trueThe flutter_localizations dependency uses the Flutter SDK. It should not be added with a version number from pub.dev.
The intl package is a regular Dart package. Using any lets Flutter resolve a compatible version required by flutter_localizations. In a project with a strict dependency policy, you can use a compatible pinned version instead.
The generate: true setting tells the Flutter tool that the project uses generated localization code. Later, Flutter will turn ARB translation files into a Dart class named AppLocalizations.
| Setting | Purpose |
|---|---|
| `flutter_localizations` | Supplies Flutter's built-in localized widget text |
| `intl` | Formats dates, numbers, and locale-sensitive messages |
| `generate: true` | Enables Flutter localization code generation |
Fetch packages and check the setup
Save pubspec.yaml, then run:
flutter pub get
flutter gen-l10nflutter pub get resolves dependencies and updates the project's lock file. flutter gen-l10n looks for localization configuration and ARB files. If those files do not exist yet, generation may report that there is no input to process. That is expected at this stage.
After adding the localization files in later chapters, run flutter gen-l10n again. You can also run flutter run; with generate: true, Flutter can generate localization code as part of its normal build process.
Understand the dependency relationship
The generated localization class will use APIs from intl. Flutter's built-in delegates also depend on localization support from the Flutter SDK. Adding both dependencies makes the project's intent clear and allows the generated code to compile.
The setup does not translate any text by itself. It only prepares the project. You still need to create ARB files, configure gen-l10n, and connect the generated delegates to MaterialApp.
Exercise
Which dependency provides Flutter's localized Material and Cupertino widget text, and which dependency formats dates and numbers?
Show answer
flutter_localizations provides Flutter widget localization, while intl formats dates, numbers, and other locale-sensitive values.