Sub topic 3
Configuring gen-l10n with l10n.yaml
This chapter assumes you know basic Dart null safety, can build a Flutter app with MaterialApp, and can edit YAML and JSON files. You should also know how to run flutter pub get.
Flutter's gen-l10n tool converts ARB translation files into Dart code. The generated code contains an AppLocalizations class that your widgets can use. The l10n.yaml file controls where Flutter finds the ARB files and where it writes the generated Dart file.
Add the localization dependencies
First, enable localization code generation in pubspec.yaml:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: any
flutter:
generate: trueflutter_localizations provides Flutter's built-in translated Material and Cupertino messages. The intl package provides locale-aware formatting and message support. The generate: true setting enables Flutter's localization generation workflow.
Run this command after editing the file:
flutter pub getCreate l10n.yaml
Create l10n.yaml in the Flutter project's root directory, next to pubspec.yaml:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: falseEach setting has a specific role:
| Setting | Meaning |
|---|---|
| `arb-dir` | Directory containing the ARB files |
| `template-arb-file` | ARB file that defines the source messages and metadata |
| `output-localization-file` | Name of the generated Dart file |
| `synthetic-package` | Whether the generated file is placed in a synthetic package |
With this configuration, Flutter reads files from lib/l10n. Because synthetic-package is false, it writes the generated file into that same directory:
lib/
l10n/
app_en.arb
app_es.arb
app_localizations.dartThe template file is usually the source-language file. Here, app_en.arb is the template, and app_es.arb adds Spanish translations.
Understand the generation flow
The configuration connects your source files to generated Dart code:
Flutter can run generation as part of common Flutter commands when generate: true is enabled. You can also run it explicitly:
flutter gen-l10nIf the command succeeds, inspect the generated file. It should contain AppLocalizations, supported locales, and localization delegates. Do not edit this generated file manually. Change the ARB files or l10n.yaml, then generate it again.
Common configuration errors
- A misspelled
arb-dircauses Flutter to find no translation files. - A
template-arb-filename that does not exist stops generation. - Placing
l10n.yamlinlib/prevents Flutter from using it as the project configuration. - Forgetting
generate: truecan prevent automatic generation. - Editing generated Dart code loses your changes the next time generation runs.
Exercise
Your ARB files are in lib/translations, the template is messages_en.arb, and you want the generated file at lib/translations/localizations.dart. Which l10n.yaml values should you use?
Show answer
Use arb-dir: lib/translations, template-arb-file: messages_en.arb, output-localization-file: localizations.dart, and synthetic-package: false.