Sub topic 2
Adding flutter_localizations and intl to the project
This chapter assumes you can read and edit pubspec.yaml, run flutter pub get, and build a basic MaterialApp. It covers only the project setup. The l10n.yaml file, the ARB format, and the generated class come in later chapters.
What the setup has to do
Flutter's localization tooling needs three things from your project:
- The
flutter_localizationspackage, which supplies translations for Flutter's own widgets. - The
intlpackage, which supplies message formatting, plural rules, and date and number formats. - A switch in
pubspec.yamlthat tells the Flutter tool to run code generation for you.
Each item is small. Mistakes in any of them produce errors that look unrelated to the cause, such as an import that does not resolve. Knowing what each piece does makes those errors easy to trace.
What each package contributes
flutter_localizations ships with the Flutter SDK. It is not downloaded from pub.dev. It contains the localized strings and formatting that Flutter's built-in widgets use: the "OK" and "Cancel" buttons in dialogs, the month names in the date picker, the "Back" tooltip in the app bar, and the text direction for right-to-left languages. It also supplies the delegates (the objects that load this data) for the Material, Cupertino, and widgets libraries.
intl is a Dart package maintained by the Dart team. It provides DateFormat, NumberFormat, the ICU message syntax for plurals and selects, and the Intl class that tracks the current locale. The code that gen-l10n generates calls into intl, so the generated file imports it.
Neither package translates your own app's text. That comes from your ARB files, and gen-l10n turns them into the AppLocalizations class.
Adding the dependencies
You can add both packages from the command line:
flutter pub add flutter_localizations --sdk=flutter
flutter pub add intl:anyThe first command writes flutter_localizations: sdk: flutter under dependencies. The second writes intl: any. You can also edit pubspec.yaml by hand. A complete file for a small project looks like this:
name: trip_planner
description: A small app used to practice localization.
publish_to: "none"
version: 1.0.0+1
environment:
sdk: ^3.5.0
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: any
flutter:
uses-material-design: true
generate: trueAdjust the sdk constraint to match the Dart version your Flutter installation ships with. Everything else is what this chapter is about.
Why `intl: any`
flutter_localizations depends on a specific intl version and pins it tightly. Each Flutter release moves that pin. If you write intl: ^0.19.0 and your Flutter SDK expects a different release, flutter pub get fails with a version-solving error that names both packages.
Writing intl: any hands the decision to the resolver. It picks the one version that satisfies flutter_localizations, and you do not have to update the line on each Flutter upgrade. The cost is that your constraint no longer documents a version, and the resolved version is visible only in pubspec.lock. Check it whenever you need to know:
flutter pub deps --style=compact | grep intlSome teams pin a caret range anyway and update it when they upgrade Flutter. That works too, as long as you accept the occasional resolver error right after an SDK upgrade. any is the choice that gives the fewest surprises.
The `generate: true` flag
The line generate: true belongs under the top-level flutter: key. It tells the Flutter tool to run gen-l10n whenever it prepares the project. That happens during flutter pub get, flutter run, and flutter build. Without the flag, nothing generates the localization code. You then have ARB files and no AppLocalizations class, and the import in your app fails.
You can also run the generator by hand:
flutter gen-l10nRunning it by hand is useful when you want to see the generator's error messages in isolation, or when your editor shows stale errors after you edit an ARB file. The flag is still needed for automatic generation on normal builds.
One `flutter:` block only
Most projects already have a flutter: section containing uses-material-design: true and possibly assets: or fonts:. Add generate: true to that section. A second flutter: key at the top level is invalid YAML, because mapping keys must be unique. Depending on the parser, either an error appears or one of the two blocks is silently dropped.
Indentation matters as well. The key must be indented by two spaces under flutter:. If it sits at the top level, the tool ignores it.
# Wrong: generate is at the top level, so the Flutter tool never sees it.
flutter:
uses-material-design: true
generate: true# Right: generate is a child of the flutter key.
flutter:
uses-material-design: true
generate: trueAdding the first files
Setup is not complete until gen-l10n has something to read. With the defaults, the generator looks for a template ARB file in lib/l10n. Create a minimal one at lib/l10n/app_en.arb:
{
"@@locale": "en",
"appTitle": "Trip Planner"
}You will extend this file in the ARB chapter. For now it only needs to exist and be valid JSON. If the folder is empty, generation fails with a message saying the template file cannot be found.
Then run:
flutter pub getIf the setup is right, a file named app_localizations.dart appears. Its location depends on the synthetic-package option, which the next chapter explains. In current Flutter versions the generated files are written into the ARB directory, so you would see lib/l10n/app_localizations.dart. In older projects the generated code lived in a hidden synthetic package, and you imported it as package:flutter_gen/gen_l10n/app_localizations.dart. If you see that import in an older tutorial, the setup steps in this chapter are the same. Only the import path differs.
Checking that the setup works
A quick check is to import the generated file and read the title. This test needs no MaterialApp wiring. It only confirms that the class exists and compiles:
import 'package:flutter_test/flutter_test.dart';
import 'package:trip_planner/l10n/app_localizations_en.dart';
void main() {
test('generated English class returns the template text', () {
final l10n = AppLocalizationsEn();
expect(l10n.appTitle, 'Trip Planner');
});
}AppLocalizationsEn is one of the per-language classes that gen-l10n creates next to AppLocalizations. The file name follows the pattern app_localizations_<language>.dart. If your package is not called trip_planner, change the import accordingly. Run the test with flutter test.
Common errors and what they mean
| Symptom | Likely cause | Fix |
|---|---|---|
| `Target of URI doesn't exist` for `app_localizations.dart` | `generate: true` missing, misplaced, or no `pub get` since adding it | Put the flag under `flutter:` and run `flutter pub get` |
| Version solving failed, mentioning `intl` and `flutter_localizations` | `intl` pinned to a version the SDK does not allow | Use `intl: any`, or the version the error message suggests |
| `flutter_localizations` not found | Added as `flutter_localizations: ^1.0.0` instead of an SDK dependency | Use `sdk: flutter` |
| gen-l10n reports no template ARB file | No `lib/l10n/app_en.arb`, or the folder is somewhere else | Create the file, or set `arb-dir` in `l10n.yaml` |
| Built-in widgets still show English in other locales | Delegates not passed to `MaterialApp` | Covered in the wiring chapter |
| Generated file missing in CI | CI does not run `pub get`, or you committed the output unevenly | Run `flutter pub get` before building |
Dev dependency mistakes
Do not put flutter_localizations or intl under dev_dependencies. Both are imported by code that ships in your app, directly or through the generated file. Dev dependencies are not available in release builds. The analyzer may accept the code in debug mode and then fail on a release build.
Should you commit the generated file
Teams disagree. If the generated file is not committed, every developer and CI job must run flutter pub get before analysis or tests, which they normally do anyway. If you do commit it, reviewers see the generated diff for each ARB change, and you must keep it in sync. Pick one convention and state it in your contributing guide. Mixing the two is what causes the "works on my machine" failures.
Exercises
Exercise
After adding flutter_localizations and intl: any to pubspec.yaml, your colleague's app still cannot import app_localizations.dart after flutter pub get. Their pubspec.yaml has generate: true as a top-level key, next to flutter: rather than inside it. What is wrong, and how do you fix it?
Show answer
The Flutter tool only reads generate as a child of the flutter: key. At the top level it is ignored, so gen-l10n never runs. Move it under flutter:, indented by two spaces, next to uses-material-design: true, then run flutter pub get again.
Exercise
Why is intl: any often recommended instead of a caret constraint such as intl: ^0.19.0, and what is the trade-off?
Show answer
flutter_localizations pins intl to a specific version that changes between Flutter releases. With any, the resolver picks the version the SDK requires, so Flutter upgrades do not cause version-solving errors. The trade-off is that pubspec.yaml no longer records the version, so you check pubspec.lock or flutter pub deps to see it.