Sub topic 3
Configuring gen-l10n with l10n.yaml
This chapter assumes you already added flutter_localizations, intl, and generate: true to pubspec.yaml, as covered in the previous chapter. You should also be comfortable reading YAML and running flutter pub get. Here you will write the l10n.yaml file that tells the generator where your translations live and what to produce.
What l10n.yaml does
The gen-l10n tool reads ARB files and writes Dart code. It needs to know three things:
- Where the ARB files are.
- Which ARB file is the template.
- Where to put the generated code and what to call it.
You answer these in a file named l10n.yaml, placed in the project root next to pubspec.yaml. With flutter: generate: true set, Flutter runs the generator during flutter pub get and flutter run. You can also run it by hand with flutter gen-l10n.
The flow looks like this:
A minimal configuration
This is the configuration used throughout the tutorial:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: falseEach line does one job.
arb-diris the folder that holds your.arbfiles. The path is relative to the project root.template-arb-filenames the file that defines every message. The generator takes message keys, placeholder types, and descriptions from this file.output-localization-fileis the name of the main generated file. The class inside it is named separately (seeoutput-classbelow).synthetic-package: falsewrites the generated files into a normal folder in your project instead of a hidden synthetic package.
The defaults for the first three keys are lib/l10n, app_en.arb, and app_localizations.dart. So this exact file is equivalent to an empty one on a current Flutter version, apart from the synthetic-package line. Writing the values out is still worthwhile. It documents your layout and protects you if you rename a folder.
Where the generated files go
With synthetic-package: false, the generator writes into arb-dir unless you set output-dir. For the configuration above you get:
lib/l10n/
app_en.arb
app_es.arb
app_localizations.dart
app_localizations_en.dart
app_localizations_es.dartThe main file, app_localizations.dart, defines the abstract AppLocalizations class and its delegate. Each app_localizations_<locale>.dart file holds the implementation for one locale. You import only the main file:
import 'l10n/app_localizations.dart';Because the files sit inside lib, you import them with a relative or package: path like any other Dart file. The older synthetic-package mode wrote them into a generated package and you imported package:flutter_gen/gen_l10n/.... Recent Flutter releases deprecate that mode, so new projects should set synthetic-package: false. Files that live in your source tree are also visible to your IDE, to code search, and to version control.
Once you choose a location, decide whether to commit the generated files:
| Choice | Benefit | Cost |
|---|---|---|
| Commit the generated Dart files | The project builds without running the generator. Reviewers see the diff. | Noisy diffs, and the files can drift if someone forgets to regenerate. |
| Add them to `.gitignore` | The repository holds only the sources of truth. | Every checkout and CI job must run `flutter pub get` or `flutter gen-l10n` first. |
Either choice works. Pick one per project and make CI enforce it.
Separating the output folder
Setting output-dir keeps generated code away from the files translators edit:
arb-dir: lib/l10n
output-dir: lib/l10n/generated
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: falseYour import becomes import 'l10n/generated/app_localizations.dart';. This layout makes it simple to ignore only the generated folder. output-dir applies only when synthetic-package is false.
Other useful options
The following options are optional. They are all keys in l10n.yaml.
| Key | Default | Purpose |
|---|---|---|
| `output-class` | `AppLocalizations` | Name of the generated class. Rename it if the default clashes with another class. |
| `nullable-getter` | `true` | When `true`, `AppLocalizations.of(context)` returns a nullable type, so you write `!`. When `false`, it returns a non-null type. |
| `preferred-supported-locales` | none | Orders the generated `supportedLocales` list. The first entry is the fallback used when the device locale matches nothing. |
| `untranslated-messages-file` | none | Writes a file that lists, per locale, the message keys that are missing a translation. |
| `header` / `header-file` | none | Text added at the top of each generated file, such as a license notice. |
| `use-escaping` | `false` | Lets you quote literal braces in messages with single quotes. |
| `format` | `false` | Runs the Dart formatter on the generated files. |
A configuration that uses several of them:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
output-class: AppStrings
synthetic-package: false
nullable-getter: false
preferred-supported-locales: [en, es]
untranslated-messages-file: build/untranslated.jsonWith this file, the generated class is AppStrings, and widgets call AppStrings.of(context) with no !. The code in the rest of this tutorial uses the defaults, so it writes AppLocalizations.of(context)!.
Running the generator and reading its output
Run the tool by hand to see errors immediately:
flutter gen-l10nIf the configuration is valid, the command prints nothing and writes the Dart files. If it fails, the message names the file and the problem. Typical failures are listed below.
- Template file not found.
template-arb-filenames a file that is not insidearb-dir. The value is a file name, not a path. Writeapp_en.arb, notlib/l10n/app_en.arb. - Wrong directory.
arb-dirpoints at a folder that does not exist, or you created the ARB files underlib/l10nwhile the config sayslib/localization. - No generated code appears.
generate: trueis missing from theflutter:section ofpubspec.yaml. Without it,flutter rundoes not invoke the generator. - Import fails after generating. The import path does not match
output-dir, or you leftsynthetic-packageat a value that places the files elsewhere than you expect. - YAML syntax error. Keys in
l10n.yamluse hyphens (arb-dir), not underscores or camel case. A misspelled key is easy to miss, so compare the names to the table above.
Choosing a template locale
The template file is usually your source language, such as app_en.arb. The locale of the template also matters in two ways. It is the locale whose metadata (descriptions and placeholder types) the generator trusts. Translated files do not need to repeat that metadata. And a team that writes its copy in Spanish first can set template-arb-file: app_es.arb and treat English as one more translation. Nothing in gen-l10n requires English.
Keep the file naming consistent. The generator reads the locale from @@locale inside the file, or from the suffix in the file name (app_es.arb gives es). A file whose name and @@locale disagree is a common source of confusing errors.
Exercise
A project has ARB files in lib/l10n, and you want the generated Dart files in lib/l10n/generated so that you can add that folder to .gitignore. Write the l10n.yaml file, and give the import line the app uses.
Show answer
arb-dir: lib/l10n
output-dir: lib/l10n/generated
template-arb-file: appen.arb
output-localization-file: applocalizations.dart
synthetic-package: false
The app imports the file with import 'l10n/generated/app_localizations.dart'; (relative to the file in lib). output-dir takes effect only because synthetic-package is false.
Exercise
You run flutter run and no generated code appears, although l10n.yaml is correct. Name the most likely cause and the fix.
Show answer
The generate flag is missing from pubspec.yaml. Add generate: true under the flutter: section and run flutter pub get (or run flutter gen-l10n directly).