Back to Articles

Intermediate

Multi-Language Support in Flutter with Localization (l10n)

· 8 min read

Why .arb Files?

Writing every user-facing string directly in your code turns adding a new language, or even changing a single sentence, into a code change. Flutter's official solution is .arb (Application Resource Bundle) files: one JSON-based file per language, converted into a type-safe Dart class by the flutter gen-l10n command. If you misspell a key, you get a compile-time error instead of a runtime crash.

l10n.yaml Configuration

l10n.yaml, placed at the project root, defines where the .arb files live, which one is the template, and what the generated class should be called. The template file (usually the one in your main language, here tr) defines the set of keys every other .arb file must match.

arb-dir: lib/l10n
template-arb-file: app_tr.arb
output-localization-file: app_localizations.dart
output-class: AppLocalizations
synthetic-package: false

Writing the .arb Files

Each .arb file is plain JSON. The @key block under a key carries metadata like placeholder types and a description; this metadata explains the context to whoever translates it — or to future you.

{
  "@@locale": "en",
  "welcomeTitle": "Welcome",
  "@welcomeTitle": {
    "description": "Title on the welcome screen"
  },
  "greeting": "Hello, {name}!",
  "@greeting": {
    "placeholders": {
      "name": { "type": "String" }
    }
  }
}

flutter gen-l10n and AppLocalizations.of(context)

The flutter gen-l10n command reads l10n.yaml and generates the AppLocalizations class from each .arb file. Every key becomes a Dart method or getter, complete with its placeholder parameters, so your IDE's autocomplete shows exactly which strings are available.

Text(AppLocalizations.of(context)!.welcomeTitle)

Text(AppLocalizations.of(context)!.greeting(user.name))

Plurals and Conditional Text: ICU Syntax

Instead of hand-rolling if/else logic for plural rules that vary by language — "1 unread message" vs "5 unread messages" — .arb files support ICU plural syntax. gen-l10n automatically turns this syntax into a Dart function that follows the correct grammar rules for each language.

{
  "unreadCount": "{count, plural, =0{No unread messages} one{1 unread message} other{{count} unread messages}}",
  "@unreadCount": {
    "placeholders": {
      "count": { "type": "num", "format": "compact" }
    }
  }
}
Text(AppLocalizations.of(context)!.unreadCount(unreadMessages.length))

supportedLocales and Delegates in MaterialApp

The generated AppLocalizations class ships its own localizationsDelegates and supportedLocales lists ready to use; you just wire them directly into MaterialApp. Flutter matches the device's system language against supportedLocales and automatically picks the right .arb translation.

MaterialApp(
  localizationsDelegates: AppLocalizations.localizationsDelegates,
  supportedLocales: AppLocalizations.supportedLocales,
  locale: _currentLocale,
  home: const HomePage(),
)

Switching Language at Runtime

Besides auto-detecting the system language, if you want to let users switch language from inside the app, you control MaterialApp's locale parameter with your own state. Since it's not sensitive data, SharedPreferences is enough to persist the choice.

class LocaleController extends ValueNotifier<Locale?> {
  LocaleController() : super(null);

  Future<void> setLocale(Locale locale) async {
    value = locale;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('locale_code', locale.languageCode);
  }
}

Things to Watch Out For

• Remember to add a key to EVERY language's .arb file for every new user-facing string; a missing key falls back to the template language (usually English) for that locale. • Remember to rerun flutter gen-l10n every time you change the .arb files, or AppLocalizations will stay stale. • Define placeholder types (String, num, DateTime) correctly in the .arb file; the wrong type causes a compile error in the generated Dart code. • Only localize user-facing text; strings only a developer sees, such as debug log messages or internal error codes, don't need to go into .arb.

Conclusion

Starting from l10n.yaml, filling in .arb files, generating with flutter gen-l10n and consuming with AppLocalizations.of(context) lets you avoid hardcoded strings entirely. ICU plural support means you don't have to write grammar rules yourself; wire the right delegates and supportedLocales list into MaterialApp and the system language is detected automatically, with a user preference layered on top if you want it.