Sub topic 4
Writing ARB files: messages, descriptions, and metadata
This chapter assumes you can read JSON, have a Flutter project with flutter_localizations and intl added, and have an l10n.yaml that points at lib/l10n with app_en.arb as the template. If any of that is missing, finish the setup chapters first. Here you will learn how an ARB file is structured, what metadata it needs, and which mistakes make flutter gen-l10n fail or produce surprising output.
What an ARB file is
ARB stands for Application Resource Bundle. It is a JSON file in which each key is a message ID and each value is the text for one locale. Keys that start with @ carry metadata about a message. Keys that start with @@ carry metadata about the whole file.
You write one ARB file per locale. One of them is the template. The template-arb-file setting in l10n.yaml names it. The generator reads the template to learn which messages exist, what their placeholders are, and which Dart method signatures to produce. Every other ARB file only supplies translated text.
A minimal template file
This is the template for the running Trip Planner example. Save it as lib/l10n/app_en.arb.
{
"@@locale": "en",
"appTitle": "Trip Planner",
"@appTitle": {
"description": "Title shown in the app bar and task switcher"
},
"greeting": "Hello, {name}!",
"@greeting": {
"description": "Greeting shown on the home screen",
"placeholders": {
"name": {
"type": "String",
"example": "Ada"
}
}
},
"tripCount": "{count, plural, =0{No trips} =1{1 trip} other{{count} trips}}",
"@tripCount": {
"description": "Number of trips the user has planned",
"placeholders": {
"count": {
"type": "int"
}
}
},
"departure": "Departs on {date}",
"@departure": {
"description": "Departure date of a trip",
"placeholders": {
"date": {
"type": "DateTime",
"format": "yMMMd"
}
}
}
}The file has four messages: appTitle, greeting, tripCount, and departure. Each one is a message entry (the key and its text) followed by a metadata entry (the same key with an @ prefix).
Message keys
A message key becomes a Dart identifier. A key such as appTitle becomes a getter on AppLocalizations. A key with placeholders becomes a method.
Follow these rules:
- Start with a lowercase letter.
- Use only letters, digits, and underscores. Camel case is the convention.
- Do not use a Dart reserved word such as
classordefault. - Keep keys unique inside a file. JSON allows duplicate keys, but the parser keeps only one of them, so a duplicate silently discards a message.
Name keys by what the text means in the UI, not by the English wording. checkoutButton survives a copy change. buyNow becomes misleading when marketing switches the label to "Pay".
Descriptions
The description field in the metadata is for translators. It does not affect the generated code, other than appearing in the doc comment of the generated getter or method. Translators often see a string without the screen it appears on, so the description is how you give them context.
A useful description says where the text appears, what it refers to, and any length limit. Compare these:
- Weak:
"description": "Title" - Better:
"description": "Title shown in the app bar and task switcher"
The word "Save" is a classic case. In English it is a verb on a button, but a translator needs to know whether it means saving a file or rescuing someone. The description removes the guess.
Placeholders
A placeholder is a named slot in the message text, written in braces, such as {name}. Each placeholder used in a message should be declared in the placeholders map of the template's metadata.
Each declaration can have these fields:
| Field | Purpose |
|---|---|
| `type` | The Dart type of the parameter: `String`, `int`, `double`, `num`, `DateTime`, or `Object`. |
| `example` | A sample value shown to translators and in generated docs. |
| `description` | A note about the placeholder. |
| `format` | A formatting name, used with `DateTime` and number types. |
| `optionalParameters` | Extra arguments for number formats, such as `decimalDigits`. |
| `isCustomDateFormat` | Set to `"true"` when `format` is a custom pattern rather than a named one. |
The type matters because it defines the generated method signature. For greeting, the generator emits roughly String greeting(String name). For tripCount it emits String tripCount(int count). Passing a String where an int is expected is a compile-time error, not a runtime surprise.
If you omit type, the parameter defaults to Object, and the generator calls toString() on it. That compiles but loses type checking, so declare types.
A placeholder that appears in the text but is not declared in the template metadata may produce a warning or an unhelpful error, depending on your Flutter version. Declare every placeholder.
Plurals
A plural message uses ICU syntax. The text starts with the placeholder name, the keyword plural, and then a set of cases:
"tripCount": "{count, plural, =0{No trips} =1{1 trip} other{{count} trips}}"The cases =0 and =1 match exact numbers. The keywords zero, one, two, few, many, and other match plural categories that depend on the language. The other case is required, and it is the fallback. The count placeholder must be declared as a number type, usually int.
Plural syntax gets its own chapter. For now, remember that the whole expression is one JSON string, and the inner {count} is a placeholder inside a case.
Dates with a format
The departure message shows a DateTime placeholder with "format": "yMMMd". The value yMMMd is an intl skeleton, which renders as "Mar 14, 2026" in English and a different shape in other locales. Because the format lives in the template metadata, translators do not need to repeat it.
If you need a pattern that is not a named skeleton, set isCustomDateFormat to "true" and write the pattern in format. Note that the value is the string "true", not a JSON boolean.
Translated files
A translated file repeats the keys with translated text. It does not need metadata, because the generator takes that from the template. This is lib/l10n/app_es.arb:
{
"@@locale": "es",
"appTitle": "Planificador de viajes",
"greeting": "¡Hola, {name}!",
"tripCount": "{count, plural, =0{Sin viajes} =1{1 viaje} other{{count} viajes}}",
"departure": "Sale el {date}"
}Two rules keep translations safe:
- Placeholder names must be identical to the template's. Translating
{name}to{nombre}breaks the build or the output. - The translation may reorder placeholders freely. A language whose grammar puts the date first can do so, because placeholders are named, not positional.
A translated file may omit keys. The generator then falls back to the template's text for that locale, and reports the missing keys. A later chapter shows how to turn that report into a to-do list.
The `@@locale` entry
The @@locale entry states which locale the file holds. It is optional if the file name carries the locale, as in app_es.arb, but writing it avoids ambiguity. If the two disagree, the generator reports an error. Regional variants use the language_COUNTRY form in the name, for example app_pt_BR.arb with "@@locale": "pt_BR".
Special characters and escaping
ARB values are JSON strings with extra meaning for braces and the apostrophe.
- Write a literal brace by wrapping it in single quotes:
'{'and'}'. - Write a literal apostrophe as two apostrophes,
'', in messages that also contain placeholders. This follows ICU rules. - Use
\nfor a line break, as in any JSON string. - Save the file as UTF-8 so characters such as
¡andésurvive.
Common errors
| Symptom | Likely cause |
|---|---|
| Generation fails with a JSON parse error | A trailing comma, a missing quote, or a comment. JSON has no comments. |
| A message is missing from `AppLocalizations` | The key exists only in a translated file. Messages come from the template. |
| Method takes `Object` instead of `int` | The placeholder has no `type` in the template metadata. |
| Plural fails to parse | The `other` case is missing, or braces do not balance. |
| Wrong text for one locale | `@@locale` does not match the file name, or the translation uses a renamed placeholder. |
Checking your work
After editing ARB files, run the generator by hand:
flutter gen-l10nRead the output. Errors name the file and the key that failed. Fix the template first, because an error there stops generation for every locale.
Exercise
You add the key "cancelButton" to appes.arb but forget to add it to appen.arb, the template. What happens, and where should the key and its metadata be added?
Show answer
The generator builds the message list from the template, so "cancelButton" is not generated on AppLocalizations and the Spanish entry is ignored or reported as having no template counterpart. Add the key, its English text, and its metadata (at least a description) to app_en.arb, then run flutter gen-l10n again.
Exercise
A message is written as "{count, plural, =1{1 item} other{{count} items}}" and the metadata declares count without a type. What type does the generated method use for count, and how do you get compile-time checking?
Show answer
Without a type the parameter is Object, so the compiler cannot catch a wrong argument. Add "type": "int" to the count placeholder in the template's metadata so the method takes an int.