Sub topic 11
How Flutter chooses a locale and falls back
This chapter assumes you have a working gen-l10n setup. That means l10n.yaml, at least two ARB files, and AppLocalizations wired into MaterialApp. You should also be comfortable with Locale, which pairs a language code such as es with an optional script and country code such as MX.
Having translations is not enough. The app must also decide which translation to show. This chapter covers how that decision is made, what the fallback is, and where it goes wrong.
Where the preferred locales come from
The operating system gives Flutter an ordered list of the user's preferred locales. On modern Android and iOS this can hold several entries, for example [fr_FR, es_ES, en_US]. Older platforms may supply only one.
Flutter compares that list with the supportedLocales you pass to MaterialApp. The result is a single Locale that Localizations uses to load every delegate. You can read the result at any point below MaterialApp:
final Locale resolved = Localizations.localeOf(context);The resolved locale is always one of your supportedLocales, unless you force one with the locale property or write a callback that returns something else.
The resolution pipeline
MaterialApp passes its settings to WidgetsApp, which resolves the locale in this order:
- If you set the
localeproperty, that locale is used and the device list is ignored. This is how a language picker works, and the next chapter covers it. - Otherwise, if you provided
localeListResolutionCallback, it receives the full preferred list and your supported locales. - Otherwise, if you provided
localeResolutionCallback, it receives only the first preferred locale. - Otherwise, Flutter runs its built-in algorithm.
The default matching algorithm
The built-in algorithm walks the user's preferred locales from most to least preferred. For each one it tries the following, from strongest match to weakest:
- An exact match on language, script, and country.
- A match on language and script, if the user locale has a script.
- A match on language and country, if the user locale has a country.
- A match on language alone.
A language-only match is held back rather than returned at once. When the user's top preference matches only by language, the algorithm still checks whether the next preference shares that language, because that entry might match more precisely. Once a language-only match exists, it is kept. A weaker match on a lower-ranked locale cannot replace it.
If no preferred locale matches by language, Flutter may fall back to a country-only match. If that also fails, it returns the first entry of supportedLocales.
Suppose supportedLocales is [en, es, es_MX, pt_BR]. The table shows what the default algorithm does for different devices:
| Device preferred locales | Resolved locale | Reason |
|---|---|---|
| `es_MX` | `es_MX` | Exact match on language and country |
| `es_AR` | `es` | Language match. The first supported `es` entry is used |
| `pt_PT` | `pt_BR` | Language match. `pt_BR` is the only Portuguese entry |
| `fr_FR` | `en` | No match, so the first supported locale is used |
| `fr_FR`, `es_ES` | `es` | The French entry fails, so the Spanish language match wins |
The fallback is the first supported locale
The last row of the table is the main trap. A French user gets English only because en happens to come first in supportedLocales. That order is not something you wrote by hand. AppLocalizations.supportedLocales is generated from the ARB files you have. If another locale ends up first, every unmatched user sees that language.
Check the generated file when you rely on this fallback. You can also set the order in l10n.yaml:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
preferred-supported-locales: [en]preferred-supported-locales moves the listed locales to the front of the generated supportedLocales. The first one becomes the fallback.
For stronger guarantees, write the fallback in code with a callback, as shown below.
Regional variants in ARB files
A file named app_es_MX.arb defines the es_MX locale. The generated code produces a class for es_MX that extends the Spanish class. You only need to put the messages that differ in the regional file. Everything else is inherited from app_es.arb.
Keep a language-only file (app_es.arb) next to any regional file. Without it, users from other Spanish-speaking countries have no Spanish to fall back to, and gen-l10n reports a problem.
At runtime, the generated delegate loads a locale in two steps. It first tries the full locale, such as es_MX. It then tries the language code alone. Its isSupported method looks only at the language code. So the delegate reports that it supports es_AR even though no es_AR messages exist, and it loads the Spanish class for it.
Writing your own resolution
Add a callback when the defaults don't fit your product. A common case is wanting a particular fallback regardless of list order. Another is mapping a family of locales to one of your own, such as sending all Portuguese to pt_BR.
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';
const Locale fallbackLocale = Locale('en');
Locale resolveLocales(
List<Locale>? preferred,
Iterable<Locale> supported,
) {
if (preferred == null || preferred.isEmpty) {
return fallbackLocale;
}
for (final Locale user in preferred) {
// Prefer an exact language and country match.
for (final Locale candidate in supported) {
if (candidate.languageCode == user.languageCode &&
candidate.countryCode == user.countryCode) {
return candidate;
}
}
// Then accept any entry with the same language.
for (final Locale candidate in supported) {
if (candidate.languageCode == user.languageCode) {
return candidate;
}
}
}
return fallbackLocale;
}
class LocaleDemoApp extends StatelessWidget {
const LocaleDemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
localeListResolutionCallback: resolveLocales,
home: Builder(
builder: (context) => Scaffold(
body: Center(
child: Text(
'Resolved: ${Localizations.localeOf(context)}',
),
),
),
),
);
}
}localeListResolutionCallback receives a nullable list, because some platforms report no preferences, so the function handles null explicitly. It returns the fallback in both cases. Because it is a plain function, you can unit test it without building any widgets.
Prefer localeListResolutionCallback over localeResolutionCallback. The single-locale version sees only the first preference, so it discards the user's second and third choices.
Material and Cupertino must support the locale too
Your app delegates are not the only ones in play. AppLocalizations.localizationsDelegates also includes the Material, Cupertino, and widgets delegates. These supply strings such as the "OK" on a date picker and the text direction.
If you list a locale in supportedLocales that those delegates do not cover, widgets like showDatePicker can fail with a missing MaterialLocalizations error. For a language Flutter doesn't ship, add your own delegate for MaterialLocalizations or choose a close supported language.
Common errors
- The wrong language appears for unmatched users. The cause is a fallback you didn't choose. Fix it with
preferred-supported-localesor a callback. - The device is in a supported language, but the app shows the fallback. Check that the locale is in
supportedLocales. Then check the platform configuration, which the next chapters cover. On iOS, an app that doesn't declare its languages may never see them. - A callback returns a locale that is not in
supportedLocales. The app builds, but the app delegate may not load it and you get exceptions. Return only supported locales. - A hot reload doesn't change the language. Locale resolution runs when the system locale changes or the widget tree is rebuilt with new settings. To test another language, change the device language or set the
localeproperty.
Summary
Resolution has four steps: the explicit locale, then the list callback, then the single-locale callback, and finally the built-in algorithm. The built-in algorithm prefers exact matches, then script, then country, then language. It ends at the first supported locale, so the order of that list decides your fallback.
Exercise
Your app has supportedLocales of [de, en, es_MX]. A user's device lists preferred locales [fr_CA, es_ES]. Which locale does the default algorithm choose, and why? What changes if you want unmatched users to see English?
Show answer
It chooses es_MX. The fr_CA entry matches nothing. The es_ES entry has no exact or country match, but es_MX shares the language es, so the language match is used. Unmatched users, such as one whose only locale is fr_CA, would get de, the first entry in the list. To make English the fallback, put en first with preferred-supported-locales: [en] in l10n.yaml, or return en from a localeListResolutionCallback when nothing matches.
Exercise
Why is localeListResolutionCallback usually a better choice than localeResolutionCallback?
Show answer
The list version receives every preferred locale, in order. The single-locale version receives only the first one, so a user whose second choice is supported but whose first is not gets the fallback instead.