Sub topic 3
Plural and select messages
This chapter assumes you have a working gen-l10n setup: an app_en.arb template, l10n.yaml, and AppLocalizations wired into MaterialApp. You should also know Dart null safety and basic widget tests.
Plural and select messages
An ICU plural picks text by a number. An ICU select picks text by a string value.
"tripCount": "{count, plural, =0{No trips} =1{1 trip} other{{count} trips}}",
"@tripCount": {
"placeholders": { "count": { "type": "int" } }
},
"inviteText": "{gender, select, female{She invited you} male{He invited you} other{They invited you}}",
"@inviteText": {
"placeholders": { "gender": { "type": "String" } }
}Generated code exposes these as methods, such as l10n.tripCount(3).
Formatting dates and numbers
A placeholder can declare a type and a format. Flutter then formats the value for the active locale.
"departure": "Departs on {date}",
"@departure": {
"placeholders": { "date": { "type": "DateTime", "format": "yMMMd" } }
},
"price": "Price: {amount}",
"@price": {
"placeholders": { "amount": { "type": "double", "format": "decimalPattern" } }
}How Flutter chooses a locale and falls back
Flutter compares the device's preferred locales with supportedLocales. It first looks for an exact language and country match, then a language-only match. If nothing matches, it uses the first supported locale.
flowchart TD
A[Device locale list] --> B{Matches a supported locale?}
B -- Yes --> C[Use that locale]
B -- No --> D[Use first supported locale]caption: Locale resolution order alt: A flowchart. The device locale list is checked against supported locales. A match is used. No match falls back to the first supported locale.
Letting users change the language at runtime
Set MaterialApp.locale to override the device setting. Passing null returns to the device setting.
class LocaleController extends ChangeNotifier {
Locale? _locale;
Locale? get locale => _locale;
void setLocale(Locale? value) {
_locale = value;
notifyListeners();
}
}Rebuild MaterialApp with ListenableBuilder whenever the controller changes.
Declaring supported languages on iOS and other platforms
iOS shows only the languages listed in Info.plist. Add each locale you support.
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>es</string>
</array>Testing widgets under a specific locale
Set locale in the test's MaterialApp so the test does not depend on the device language.
testWidgets('shows the Spanish plural for one trip', (tester) async {
await tester.pumpWidget(MaterialApp(
locale: const Locale('es'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) => Text(AppLocalizations.of(context)!.tripCount(1)),
),
));
expect(find.text('1 viaje'), findsOneWidget);
});Keeping translations complete as the app grows
Run flutter gen-l10n --untranslated-messages-file=untranslated.json to list every key missing from a translated ARB file. Review that list before each release.
Exercise
Exercise
Your template has "{count, plural, =0{No trips} =1{1 trip} other{{count} trips}}". Which method call and argument produce "1 trip" in English?
Show answer
l10n.tripCount(1). The =1 case matches the value 1, and the placeholder count is typed as int.