Sub topic 8
Placeholders and Typed Parameters
Assume you know basic Dart syntax, null safety, JSON, and how MaterialApp loads generated localizations. A placeholder is a named value inside a translated message. Flutter turns its metadata into a typed parameter in AppLocalizations.
This keeps values separate from translated text. Translators can change word order without changing Dart code.
Define a placeholder in ARB
In an ARB file, write a placeholder inside braces. Add an @ metadata entry with the same message key. The placeholders object declares each parameter's type.
{
"@@locale": "en",
"greeting": "Hello, {name}!",
"@greeting": {
"description": "Greeting shown on the home screen",
"placeholders": {
"name": {
"type": "String",
"example": "Ada"
}
}
},
"departure": "Departs on {date}",
"@departure": {
"description": "Departure date of a trip",
"placeholders": {
"date": {
"type": "DateTime",
"format": "yMMMd"
}
}
}
}The description helps translators understand the message's purpose. The example shows the kind of value a translator should expect. The type tells gen-l10n which Dart type to use. The format tells Flutter how to format a supported value such as DateTime.
The generated class exposes these messages as methods:
final l10n = AppLocalizations.of(context)!;
Text(l10n.greeting('Ada'));
Text(l10n.departure(DateTime(2026, 3, 14)));A message without placeholders becomes a getter, such as l10n.appTitle. A message with placeholders becomes a method. If the ARB metadata declares DateTime, passing a String produces a Dart type error instead of silently creating an invalid message.
Use placeholders in translated files
A translated ARB file keeps the same message key and placeholder name. The sentence structure can change.
{
"@@locale": "es",
"greeting": "¡Hola, {name}!",
"departure": "Sale el {date}"
}The Spanish translator moved the punctuation and wording, but {name} and {date} still identify the values supplied by the application. Do not rename a placeholder in only one locale. Every translation must use the names declared by the template.
Plurals and selects also use typed values
ICU message syntax lets one message choose text based on a parameter. A plural parameter normally has type int.
{
"tripCount": "{count, plural, =0{No trips} =1{1 trip} other{{count} trips}}",
"@tripCount": {
"description": "Number of planned trips",
"placeholders": {
"count": {
"type": "int"
}
}
},
"welcome": "{role, select, admin{管理者} user{User} other{Guest}}",
"@welcome": {
"description": "Label based on the user's role",
"placeholders": {
"role": {
"type": "String"
}
}
}
}Use the generated methods like this:
Text(l10n.tripCount(3));
Text(l10n.welcome('admin'));A plural chooses a branch based on a number. A select chooses a branch based on a string. The other branch is required as a safe fallback. Translations may use different plural categories, so do not assume that every language has only singular and plural forms.
Check the generated API
Run code generation after changing ARB metadata:
flutter gen-l10nThen inspect the generated AppLocalizations API or let the Dart analyzer find mismatched arguments. Keep placeholder names and declared types consistent across all ARB files.
Exercise
What happens if an ARB message declares a count placeholder with type int, but Dart calls the generated method with a String?
Show answer
The Dart analyzer reports a type error. The call must pass an integer, such as l10n.tripCount(3).