Flutter Localization Integration: A Practical Guide
Localization is often an afterthought, but integrating it early saves hours of refactoring later. Flutter provides first-class support via the intl package and gen_l10n, but the setup has nuances that trip up even experienced developers.
This guide walks through a complete localization integration: from adding dependencies to testing translated strings, with concrete code snippets and pitfalls to avoid.
Setting Up Dependencies and Configuration
Start by adding flutter_localizations from the SDK and intl to your pubspec.yaml. Then enable code generation in your l10n.yaml file, which tells Flutter where to find ARB files and where to output generated code. A typical l10n.yaml includes arb-dir, template-arb-file, and output-localization-file. This configuration is read by the flutter gen-l10n command, which you can run manually or let run automatically during build.
After adding dependencies, run flutter pub get. You'll also need to update your MaterialApp or CupertinoApp to include localizationsDelegates and supportedLocales. The generated AppLocalizations.delegate handles the built-in widgets' translations, but you must also include GlobalMaterialLocalizations.delegate and GlobalWidgetsLocalizations.delegate for Material and Cupertino components. This ensures that date pickers, tooltips, and other built-in widgets respect the user's locale.
- Add flutter_localizations and intl to pubspec.yaml
- Create l10n.yaml with arb-dir and template-arb-file
- Run flutter gen-l10n to generate AppLocalizations
- Include delegates and supportedLocales in MaterialApp
Creating and Organizing ARB Files
ARB (Application Resource Bundle) files are JSON-based and store key-value pairs for each locale. The template file (usually app_en.arb) contains the default strings and metadata like descriptions and placeholders. For each additional locale, create a corresponding file, e.g., app_es.arb. Keys must match across files; missing translations fall back to the template, which is useful during development but can lead to untranslated strings in production if not careful.
Organize ARB files in a dedicated folder, such as lib/l10n, and keep them version-controlled. Use descriptive keys like welcomeMessage instead of msg1. For plurals and genders, use ICU syntax: {count, plural, =0{No items} =1{One item} other{{count} items}}. This syntax is supported by gen_l10n and handles complex cases without manual string concatenation. Avoid hardcoding strings in widgets; always reference AppLocalizations.of(context).
- Use one ARB file per locale, named app_<locale>.arb
- Include @key metadata for placeholders and descriptions
- Leverage ICU syntax for plurals and selects
- Keep keys consistent across all locales
| Approach | Setup Effort | Type Safety | Plural Support | Tooling |
|---|---|---|---|---|
| gen_l10n (official) | Medium | Yes | Yes (ICU) | Built-in |
| intl_utils | Medium | Yes | Yes (ICU) | Additional package |
| Manual JSON + custom | High | No | Manual | None |
| flutter_localizations only | Low | No | Limited | Built-in |
Generating and Using Localized Strings
With ARB files in place, run flutter gen-l10n (or flutter pub run intl_utils:generate if using intl_utils). This generates an AppLocalizations class with getters for each key. In your widgets, access strings via AppLocalizations.of(context)!.welcomeMessage. The generated class also provides a delegate and a list of supported locales, which you can pass directly to MaterialApp.
The generated code is type-safe: if you reference a key that doesn't exist, you'll get a compile-time error. This catches typos early. For widgets that need to update on locale change, ensure they are rebuilt when the locale changes. Flutter's Localizations widget automatically rebuilds dependent widgets when the locale changes, so using AppLocalizations.of(context) inside build methods is sufficient.
- Run code generation after modifying ARB files
- Access strings via AppLocalizations.of(context)
- Use generated supportedLocales list
- Ensure widgets rebuild on locale change
Handling Locale Resolution and Fallbacks
Flutter's locale resolution algorithm picks the best match from supportedLocales based on the user's device locale. You can customize this with localeResolutionCallback. For example, if your app supports 'en' and 'es', and the device is set to 'es_MX', Flutter will match 'es' if 'es_MX' isn't supported. If no match, it falls back to the first supported locale, which is usually the default.
Define a clear fallback strategy. In ARB files, missing keys in a non-template file fall back to the template's value. This is helpful but can mask incomplete translations. Consider adding a CI check that compares keys across ARB files to ensure completeness. For right-to-left languages, Flutter automatically flips the layout if you use Directionality and proper widgets, but test thoroughly.
- Customize locale resolution with localeResolutionCallback
- Set a sensible default locale as first in supportedLocales
- Use CI to verify ARB key completeness
- Test RTL layouts with Arabic or Hebrew
Testing Localized Apps
Widget tests can verify that strings appear correctly for a given locale. Use the Localizations widget to wrap your widget under test with specific delegates and locale. For example, pumpWidget(MaterialApp(localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, locale: Locale('es'), home: MyWidget())). Then expect(find.text('Hola'), findsOneWidget).
For integration tests, you can change the device locale programmatically or use flutter drive with a locale argument. Also, test pluralization and placeholder substitution with different values. A common pitfall is forgetting to test long strings in languages like German, which can overflow UI. Use auto-sizing text or flexible layouts to accommodate.
- Write widget tests for each supported locale
- Test plurals with zero, one, and many
- Check UI for text overflow with long translations
- Use golden tests for visual verification
Common Pitfalls and Best Practices
One frequent mistake is hardcoding strings in widgets, which bypasses localization. Always use AppLocalizations. Another is forgetting to add new ARB files to the supportedLocales list, so the locale isn't recognized. Also, be mindful of context: AppLocalizations.of(context) requires a BuildContext that has the Localizations widget as an ancestor. If you need strings outside build, pass the localization instance down.
For date and number formatting, use the intl package's DateFormat and NumberFormat with the current locale. Avoid manual formatting. When integrating with state management, ensure that locale changes trigger rebuilds; using InheritedWidget or provider patterns can help. Finally, keep translations in sync with development by using a translation management system or at least a spreadsheet for collaboration with translators.
- Never hardcode user-facing strings
- Use intl for date and number formatting
- Ensure BuildContext has Localizations ancestor
- Collaborate with translators using shared ARB files
Key points
- Enable gen_l10n early to avoid refactoring hardcoded strings.
- Use ICU syntax for plurals and placeholders to handle complex cases.
- Test with pseudo-locales and long strings to catch UI issues.
- Keep ARB files in version control and automate completeness checks.
Flutter Screen Widget and Cost Estimator
Estimate the number of widgets in a screen and a rough development-hour figure from your component counts. Adjust the fields to see how each UI area affects the total effort.
Editor and Flutter contributor
Writes practical Flutter tutorials focused on widgets, layout and animation. Enjoys breaking large UI problems into small, testable pieces and explaining them clearly to beginners.