Sub topic 4
Writing ARB Files: Messages, Descriptions, and Metadata
Assume you know basic Dart, JSON, and YAML, and that your Flutter project already uses flutter_localizations. This chapter focuses on the ARB files that gen-l10n reads.
What an ARB file contains
ARB means Application Resource Bundle. An ARB file is a JSON file that stores translated messages and information about those messages.
Use one ARB file as the template. The template defines the message keys, placeholder types, and translator guidance. Each supported language then gets its own translated ARB file.
A message has a key and a string value. The key is the stable name used by Dart code. The value is the text shown to the user.
{
"@@locale": "en",
"appTitle": "Trip Planner",
"greeting": "Hello, {name}!",
"@greeting": {
"description": "Greeting shown on the home screen",
"placeholders": {
"name": {
"type": "String",
"example": "Ada"
}
}
}
}The @@locale entry identifies the file's language. The appTitle entry has no placeholder, so it only needs a message value.
The greeting entry contains {name}. This is a placeholder, which is a named value inserted into a message. Its metadata declares that the value is a String. The example helps translators understand the value's purpose.
Message metadata
Metadata entries begin with @ and use the same key as their message. They do not create separate messages. Instead, they describe how a message should be understood or generated.
Common metadata fields include:
description, which explains where or why the message appears.placeholders, which declares values inserted into the message.type, which declares a placeholder's Dart type.example, which gives translators a realistic sample value.format, which tells Flutter how to format a value such as a date or number.
Descriptions matter because a short message can have several meanings. For example, "Open" could describe a file, a door, or an action. Write enough context for a translator to choose the correct wording.
Plurals and formatted values
ARB messages can use ICU syntax for plural choices. ICU is a message-formatting system understood by Flutter's localization generator.
{
"@@locale": "en",
"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 tripCount message selects text based on count. The departure metadata requests locale-aware date formatting. Keep the placeholder name and meaning consistent across every translation.
Writing a translated ARB file
A translated file repeats the message keys and supplies translated values. It normally does not repeat metadata because the template supplies that information.
{
"@@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}"
}Do not translate message keys or placeholder names. Translate only the user-facing text. Missing keys can cause generated-code errors or leave a message untranslated, depending on the project's configuration.
The relationship between the files looks like this:
Exercise
In an ARB message named "welcome", which part should remain unchanged in a Spanish translation: the message key, the displayed text, or both?
Show answer
The message key should remain unchanged. The displayed text should be translated. Any placeholder names inside the message should also remain unchanged.