Flutter ARB files explained: messages, placeholders and plurals
· 7 min read
Flutter's own localization tooling keeps an app's text in ARB files, short for Application Resource Bundle. They are JSON, one file per language, and Flutter turns them into Dart code you call like any other method. The format looks simple, and mostly is, but a few of its rules are easy to miss, and missing them breaks the build or, worse, shows the wrong sentence.
The setup in one minute
With flutter_localizations and intl in your dependencies and generate: true in
pubspec.yaml, a small l10n.yaml tells Flutter where the files are:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
Next to the template go the other languages: app_de.arb, app_it.arb,
app_pt_BR.arb. Note the underscore: ARB files name regions the way Dart does, not with the hyphen used
elsewhere. On every build Flutter generates an AppLocalizations class, and a widget reads a message with
AppLocalizations.of(context)!.settingsTitle.
A message and its metadata
A minimal file is a list of keys and messages:
{
"@@locale": "en",
"settingsTitle": "Settings",
"@settingsTitle": {
"description": "Title of the settings screen"
}
}
Every key can have a companion that starts with @. It holds metadata, not text: a description for
translators, and the declarations of any placeholders. @@locale says which language the file is in.
The template file is special
This is the rule people miss most often. The template, usually the English file, is the only one Flutter reads to decide which methods exist and what arguments they take. Metadata belongs there. The other files contain only the translated messages:
// app_en.arb (template)
"greeting": "Hello {name}",
"@greeting": {
"description": "Shown on the home screen",
"placeholders": { "name": { "type": "String" } }
}
// app_de.arb
"greeting": "Hallo {name}"
Two consequences:
- A key that exists in a translation but not in the template is ignored.
-
A key missing from a translation is not an error. Flutter falls back to the template's message, which means an
untranslated English sentence in a German screen. The
untranslated-messages-fileoption inl10n.yamlwrites the list of missing keys to a file, which is worth checking before a release.
Placeholders have names and types
Unlike iOS and Android, which use printf style placeholders such as %@ and
%1$s, ARB placeholders are names in braces: {name}, {count}. The name becomes
the argument of the generated method, so "Hello {name}" becomes
greeting(String name).
The type comes from the declaration in the template: String, int, double,
num or DateTime. Numbers and dates can also carry a format, such as
compact or currency for numbers, so the value is shown the way the language expects.
Names are a real advantage for translators. {name} says what goes there, where %1$@ does
not, and since every placeholder is named, word order can change freely in a translation. What translators must not
do is translate the name: {nome} in an Italian file is a different placeholder, and the build fails.
Plurals and selects use ICU syntax
Messages that depend on a number are written in ICU MessageFormat:
"filesCount": "{count, plural, =0{No files} =1{1 file} other{{count} files}}",
"@filesCount": {
"placeholders": { "count": { "type": "int" } }
}
Inside the braces come the placeholder, the word plural, and one option per case. Options are of two
kinds, and the difference matters:
-
Exact values,
=0,=1,=2, match only that number. -
Categories,
zero,one,two,few,many,other, follow the language's plural rules. In Russianonealso covers 21, 31 and 101; in Polishfewcovers 2–4 and 22–24.
An English file written with =1 works perfectly in English. Translated into Russian with the same
options, it says "21 files" with the wrong ending. A translation needs the categories of its own language, and
other is always required. (Our article on
plural forms covers the rules in detail.)
The same syntax has select, which chooses by a text value rather than a number, typically for
grammatical gender:
"invited": "{gender, select, female{She joined} male{He joined} other{They joined}}"
Keep the whole sentence inside the plural
ICU allows text around the plural block:
"deleted": "You deleted {count, plural, one{one photo} other{{count} photos}} from the album"
It is valid, but translators then have to work out which words depend on the number, and in many languages more of the sentence changes than the noun. Repeating the full sentence in each option is longer to write and much easier to translate correctly:
"deleted": "{count, plural, one{You deleted one photo from the album} other{You deleted {count} photos from the album}}"
Mistakes that break the build
- A brace left open or closed twice in a translation. ICU is strict, and one missing
}fails generation. - A plural without
other. - A placeholder renamed or translated in one language.
- The plural keyword itself translated (
plurale,Mehrzahl): it must stayplural.
All of these are easy to check automatically, and hard to spot by reading the JSON, which is a good reason not to have translators edit ARB files directly.
How Stringalize handles ARB
Stringalize imports an ARB file per language. A message made of one ICU plural, with or without text around it,
becomes a proper plural string, edited form by form in the categories of each language; =0,
=1 and =2 are read as zero, one and two. Descriptions
become notes for translators, and named placeholders stay exactly as written and are checked in every translation.
On export, the source language's file is written as Flutter's template, with descriptions and placeholder
declarations. The other files contain only translated messages, so Flutter's fallback and its list of untranslated
messages keep working. Strings that came from an iOS or Android project get names on the way out
({text}, {number}), which makes it possible to share one set of translations between a
native app and a Flutter one.
Messages built with select, or with more than one plural, are kept as text exactly as written: nothing
is lost, but they are not split into forms.