Sub topic 8
Placeholders and typed parameters
A placeholder is a named slot in an ARB message that the app fills in at run time. This chapter covers how to declare placeholders in the template file, how gen-l10n turns them into typed Dart parameters, and what can go wrong. It assumes you have a working gen-l10n setup (pubspec.yaml, l10n.yaml, a template app_en.arb, and AppLocalizations wired into MaterialApp). It also assumes you can read JSON and basic Dart.
Why placeholders exist
You could build a sentence by concatenating strings in Dart:
Text('Hello, ' + name + '!')This breaks in translation. Other languages reorder words, put the name in a different position, or need a different punctuation mark. A translator can only change text they can see in full. If the sentence lives in the ARB file with a slot for the variable part, the translator can move the slot wherever the target language needs it.
Declaring placeholders in the template
In the ARB file, wrap the slot name in curly braces. Then add a metadata entry, whose key is the message key prefixed with @. The entry declares each placeholder.
{
"@@locale": "en",
"inviteMessage": "{host} invited you to {city} for {days} days.",
"@inviteMessage": {
"description": "Invitation text on the trip detail screen",
"placeholders": {
"host": {
"type": "String",
"example": "Ada"
},
"city": {
"type": "String",
"example": "Lisbon"
},
"days": {
"type": "int",
"example": "5"
}
}
}
}Each placeholder entry can contain these fields:
typesets the Dart type of the generated parameter.exampleis a sample value. Translators and tools can show it, and it has no effect on the generated code.descriptionexplains what the value is, which helps when the name alone is unclear.formatandoptionalParametersapply a number or date format. The formatting chapter covers these in detail.
Only the template ARB file needs this metadata. Translated files repeat the message text and nothing else. The placeholder names in a translation must match the template exactly, but their position in the sentence is free:
{
"@@locale": "es",
"inviteMessage": "Durante {days} días, {host} te invita a {city}."
}Here the Spanish text puts days first, while the English text puts host first. The Dart call site does not change, because the order of the method's parameters comes from the template.
What gen-l10n generates
For each message with placeholders, AppLocalizations gets a method instead of a getter. A simplified excerpt of the generated base class looks like this:
abstract class AppLocalizations {
// Excerpt: other members omitted.
String inviteMessage(String host, String city, int days);
}Each locale gets a subclass, such as AppLocalizationsEn and AppLocalizationsEs, that builds the final string from its own text. The parameters are positional, so check the generated file if you are unsure of the exact order. Calling the method looks like this:
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';
class InviteBanner extends StatelessWidget {
const InviteBanner({super.key});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Text(l10n.inviteMessage('Ada', 'Lisbon', 5));
}
}Placeholder types
The type field accepts these values:
| ARB type | Dart parameter type | Notes |
|---|---|---|
| `String` | `String` | Inserted as is. |
| `int` | `int` | Can take a number `format`. |
| `double` | `double` | Can take a number `format`. |
| `num` | `num` | Accepts both `int` and `double`. |
| `DateTime` | `DateTime` | Can take a date `format`. |
| `Object` | `Object` | Default when `type` is omitted. |
Declare a type for every placeholder. A placeholder without a type becomes Object, so the compiler accepts any argument. Nothing then stops a caller from passing a DateTime where you meant a name, and the output will be whatever toString() returns.
With types declared, mistakes become compile-time errors:
l10n.inviteMessage('Ada', 'Lisbon', '5'); // error: String is not an int
l10n.inviteMessage('Ada', 'Lisbon'); // error: missing argumentThis is the main benefit of gen-l10n over looking strings up by key. A renamed or retyped placeholder breaks the build at every call site, not in front of a user.
Numbers and dates as placeholders
A numeric or DateTime placeholder can carry a format, so the value is rendered the way the active locale expects. Here is a numeric example:
{
"@@locale": "en",
"distanceLabel": "Distance: {meters} m",
"@distanceLabel": {
"description": "Distance of a route in meters",
"placeholders": {
"meters": {
"type": "int",
"format": "decimalPattern"
}
}
}
}With decimalPattern, the call l10n.distanceLabel(12500) produces Distance: 12,500 m in English. The same call produces 12.500 in a locale that uses a period as the thousands separator. Without format, the value is inserted with plain string interpolation, so you would get 12500 in every locale.
Date placeholders work the same way, and the date format is an ICU skeleton such as yMMMd (see the spine's departure message). The formatting chapter covers the available skeletons and number formats.
Reusing a placeholder
A placeholder can appear more than once in one message. The method takes a single parameter for it:
{
"welcomeBack": "Welcome back, {name}. Not {name}? Sign out.",
"@welcomeBack": {
"placeholders": {
"name": { "type": "String" }
}
}
}The generated method is welcomeBack(String name), and both slots receive the same value.
Literal braces
Curly braces mean "placeholder" in an ARB message, so a literal { needs escaping. Set use-escaping: true in l10n.yaml, then wrap the brace in single quotes:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
use-escaping: true{
"templateHint": "Type '{'name'}' to insert a name"
}Common errors
- Placeholder in the text but no metadata. The parameter falls back to
Object, or generation may complain, depending on your Flutter version andl10n.yamloptions. Always declare placeholders in the template. - Name mismatch in a translation. If
app_es.arbuses{nombre}but the template declares{name}, the translation does not match the generated method. Keep placeholder names identical and translate only the surrounding text. - Editing the ARB file and not regenerating. The Dart code is only updated when gen-l10n runs.
flutter pub getandflutter runtrigger it whengenerate: trueis set. You can also runflutter gen-l10nyourself. - Type mismatch with plurals. A placeholder used in a plural expression must be numeric. Declare it as
intornum, notString. - Changing a type in the template only. If you change
daysfrominttoString, every call site must change too. The compiler will list them.
Practical guidelines
- Give placeholders descriptive names such as
hostordays. Names likep1tell translators nothing. - Add an
exampleto each placeholder so translators see realistic values. - Use the narrowest type that fits:
intfor counts,DateTimefor dates,Stringonly for text. - Keep sentences whole. One message with three placeholders is easier to translate than three fragments joined in code.
Exercise
The template ARB file has the message "{count} photos from {city}" with count declared as int and city declared as String. A teammate writes l10n.photoSummary('12', 'Oslo'). What happens, and what two changes in the template would let a Norwegian translator write "Fra {city}: {count} bilder"?
Show answer
The code fails to compile, because '12' is a String and the parameter is an int. The fix is to pass 12. The template needs no change for the reordered Norwegian text, because translated ARB files may place placeholders anywhere in the sentence as long as the names match. The translator edits only app_nb.arb and keeps {count} and {city} exactly as named. The parameter order of the generated method still comes from the template.