Sub topic 15
Keeping translations complete as the app grows
This chapter assumes you have a working gen-l10n setup: a l10n.yaml, a template file lib/l10n/app_en.arb, at least one translated file such as app_es.arb, and the generated AppLocalizations class. It also assumes you can run flutter gen-l10n and read JSON. The goal is to make sure new messages reach every language, and to find the ones that don't.
What happens when a translation is missing
Suppose you add a message to app_en.arb but forget app_es.arb. The app still compiles and runs. For a locale whose ARB file lacks a key, gen-l10n generates the message using the template's text. A Spanish user sees an English sentence in the middle of a Spanish screen.
That fallback prevents crashes, but it hides the problem. Nothing fails, so the gap can ship. You need a way to list the gaps and a way to stop the build when they appear.
Reporting untranslated messages
Add one option to l10n.yaml:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
untranslated-messages-file: untranslated_messages.jsonWhen you run flutter gen-l10n, the tool writes a JSON file that maps each locale to the keys it is missing. If you add a farewell message to the template and translate it nowhere, the file looks like this:
{
"es": [
"farewell"
],
"fr": [
"farewell"
]
}Without this option, gen-l10n only prints a console warning that some messages are untranslated and suggests the option. Console warnings scroll away, so the file is the better source of truth. Add it to .gitignore if you do not want it in version control, because it is regenerated on each run.
The workflow for a new message
Follow the same sequence every time, so no step depends on memory.
- Add the key to the template file (
app_en.arb) with adescriptionand typed placeholders. - Run
flutter gen-l10n(orflutter pub getorflutter run, which also trigger generation). - Read the untranslated messages file to see which locales need the key.
- Send the new keys, with their descriptions, to translators, or add them yourself.
- Run generation again and confirm the file is empty or absent.
Descriptions matter here. A translator sees only "Save" in app_es.arb. Without the @save description, they cannot tell whether it is a button verb or a noun. Make descriptions mandatory with another l10n.yaml option:
required-resource-attributes: trueWith this set, generation fails if a template message has no metadata entry. That moves the "forgot the description" mistake from code review to the build.
Removing and renaming keys
Deleting a key from the template does not delete it from the translated files. gen-l10n only generates methods for template keys, so a stale key in app_es.arb is dead text. Over months, these accumulate and mislead translators.
Renaming a key is two changes: remove the old key from every file and add the new one. A renamed key shows up in the untranslated report for every locale. If the text did not change, copy the existing translations across, or translators redo work you already paid for.
Treat the template as the contract. A key is live if and only if it appears in app_en.arb.
Catching drift with a test
The untranslated report covers missing keys. It does not cover stale keys, or a translation whose placeholders differ from the template (for example, {nombre} instead of {name}). A plain Dart test can check both. This test reads every ARB file in lib/l10n, compares each one to the template, and fails with a readable message.
import 'dart:convert';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
Map<String, dynamic> _load(File file) =>
jsonDecode(file.readAsStringSync()) as Map<String, dynamic>;
Set<String> _messageKeys(Map<String, dynamic> arb) =>
arb.keys.where((k) => !k.startsWith('@')).toSet();
final _placeholder = RegExp(r'\{(\w+)');
Set<String> _placeholders(String message) =>
_placeholder.allMatches(message).map((m) => m.group(1)!).toSet();
void main() {
final dir = Directory('lib/l10n');
final template = _load(File('lib/l10n/app_en.arb'));
final templateKeys = _messageKeys(template);
final translations = dir
.listSync()
.whereType<File>()
.where((f) => f.path.endsWith('.arb') && !f.path.endsWith('app_en.arb'))
.toList();
for (final file in translations) {
final name = file.uri.pathSegments.last;
final arb = _load(file);
final keys = _messageKeys(arb);
test('$name has no stale keys', () {
expect(keys.difference(templateKeys), isEmpty);
});
test('$name has no missing keys', () {
expect(templateKeys.difference(keys), isEmpty);
});
test('$name uses the same placeholders as the template', () {
for (final key in keys.intersection(templateKeys)) {
final expected = _placeholders(template[key] as String);
final actual = _placeholders(arb[key] as String);
// Plural/select messages repeat the variable name, so compare sets.
expect(
actual.difference(expected),
isEmpty,
reason: '$name: "$key" uses placeholders not in the template',
);
}
});
}
}Run it with flutter test test/arb_consistency_test.dart. The regular expression finds {name}, and also finds the variable at the start of {count, plural, ...} because it matches the name right after the opening brace. It also matches the plural branch labels, such as {No trips}, which gives the words "No" in the set. To keep the check simple, the test compares only what the translation adds beyond the template, but a translated branch word could still trigger a false positive. If that happens, restrict the check to messages that have a placeholders entry in the template metadata.
Failing the build in CI
For a CI job, run generation, then check the report. A short Dart script keeps the check portable across operating systems:
import 'dart:convert';
import 'dart:io';
void main() {
final report = File('untranslated_messages.json');
if (!report.existsSync()) {
stdout.writeln('No untranslated messages report: all locales complete.');
return;
}
final content = report.readAsStringSync().trim();
if (content.isEmpty) return;
final missing = jsonDecode(content) as Map<String, dynamic>;
if (missing.isEmpty) return;
missing.forEach((locale, keys) {
stderr.writeln('$locale is missing: ${(keys as List).join(', ')}');
});
exit(1);
}Save it as tool/check_translations.dart. In CI, run flutter gen-l10n, then dart run tool/check_translations.dart. The script handles an absent file or an empty map as success, because the tool may not write the report when nothing is missing. Check once with your Flutter version and keep the script tolerant of both.
Plural rules differ by language
A message that is complete in English can be incomplete in another language. English needs one and other. Polish also uses few and many, and Arabic uses zero, one, two, few, many, and other. A translator who copies the English plural structure produces text that is wrong for some counts, and no tool will flag it.
{
"@@locale": "pl",
"tripCount": "{count, plural, =0{Brak podróży} one{{count} podróż} few{{count} podróże} many{{count} podróży} other{{count} podróży}}"
}other is always required. When you add a plural message, tell translators which categories their language needs, and put that in the message description. Review plural messages for each language that has more than two forms.
Practical habits
| Habit | What it prevents |
|---|---|
| `untranslated-messages-file` in `l10n.yaml` | Silent English fallback shipping |
| `required-resource-attributes: true` | Messages with no description for translators |
| Key-parity test | Stale keys and renamed keys left behind |
| Placeholder check | Translations that reference a variable that does not exist |
| CI step after `flutter gen-l10n` | Merging a pull request that adds untranslated keys |
| Plural categories noted in descriptions | Incorrect plural forms in languages with more than two |
Exercise
You add a message farewell to app_en.arb and forget to add it to app_es.arb. The app builds and runs without errors. What does a Spanish user see for farewell, which l10n.yaml option shows you the problem, and what file does it write?
Show answer
The Spanish user sees the English text from the template, because gen-l10n falls back to the template's message. Setting untranslated-messages-file in l10n.yaml (for example to untranslated_messages.json) makes flutter gen-l10n write a JSON file that maps each locale, here es, to its missing keys, here ["farewell"].
Exercise
Your key-parity test reports that app_es.arb contains a key oldBanner that is not in the template. Does this key affect the generated AppLocalizations class, and what should you do?
Show answer
It does not affect the generated class, because gen-l10n generates methods only from the template's keys. It is stale text left over from a deleted or renamed message. Remove it from app_es.arb (and every other translated file) so translators do not spend time on it.
Exercise
Why is a copied English plural structure (one and other only) a problem for a Polish translation, and which category is always required in every plural message?
Show answer
Polish needs the extra categories few and many to be correct for some counts, so a two-form message produces wrong text for those numbers, and nothing in the build reports it. The other category is always required.