Sub topic 11
How Flutter Chooses a Locale and Falls Back
Flutter chooses a locale by comparing the device's preferred locales with the locales your app supports. This process is called locale resolution. It decides which translations AppLocalizations should load when the app starts.
This chapter assumes that you know basic Dart null safety, can build widgets with MaterialApp, and have already configured ARB files, l10n.yaml, and generated AppLocalizations.
The locale inputs
Flutter considers three sources:
- The platform's preferred locale list, such as
es-MX, thenen-US. - The locales in
supportedLocales. - An optional
localevalue that your app sets explicitly.
The supportedLocales list is the app's contract. If the app supports only English and Spanish, Flutter should not select French even if the device prefers French.
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: AppLocalizations.localizationsDelegates,
home: const Scaffold(
body: Center(child: Text('Localized app')),
),
);
}
}With no explicit locale, Flutter normally starts with the platform's preferred locale. It then compares that locale with supportedLocales.
The matching order
Flutter's default resolver tries the most specific match first. The exact details can vary with locale scripts, but the practical order is:
- Match language, script, and country exactly.
- Match language and script.
- Match language.
- Use the first locale in
supportedLocalesas the final fallback.
For example, suppose the app supports these locales:
| Supported locale | Meaning |
|---|---|
| `en` | General English |
| `es` | General Spanish |
| `pt_BR` | Brazilian Portuguese |
A device using es-MX does not have an exact match, but it has the same language as es, so Flutter selects Spanish. A device using fr-FR has no supported language, so Flutter selects en, assuming English is first in the list.
Ordering the fallback
The first supported locale is important because it becomes the fallback. Put the default language first:
supportedLocales: const [
Locale('en'),
Locale('es'),
Locale('pt', 'BR'),
],Do not assume that the device's first locale will always be supported. Users can have several preferred languages, and the operating system may report a locale variant that your app does not provide.
> callout tip > Keep supportedLocales aligned with the ARB files that your app ships. A locale listed here without a matching translation can produce incomplete or unexpected localized content.
Custom resolution
Most apps should use Flutter's default resolver. Use localeListResolutionCallback only when the normal language, script, and country matching does not fit your product rules. For example, an app might deliberately map every Portuguese variant to Brazilian Portuguese.
A custom resolver must always return a supported locale or null. Returning an unsupported locale can prevent the localization delegates from finding the correct resources.
Exercise
If the device prefers fr-FR and the app supports en, es, and pt_BR in that order, which locale does Flutter select by default?
Show answer
Flutter selects en because French has no supported language match, so the resolver uses the first supported locale.