Flutter Localization
Localize a Flutter application: keep its strings in an ARB file, translate it in Soluling, and let Flutter compile the localized ARB files into the application.
Flutter has its own localization: the strings are in ARB files, and flutter gen-l10n generates the AppLocalizations class from them. Soluling reads the template ARB file, keeps the descriptions for the translator, opens the ICU plurals and selects in the pattern editor, and writes an ARB file for each language. Translate in Soluling Desktop or in a browser with Soluling Cloud, and update the translations over the air without a new release. The tutorial uses the Driving sample, a small driving time calculator.
- Input
- The template ARB file, such as
lib/l10n/app_en.arb - Output
- Localized ARB files, such as
app_fi.arb, next to the template - Runtime
AppLocalizations, generated byflutter gen-l10n, shows the messages of the application's locale- Cloud
- Upload the ARB file to Soluling Cloud directly, and update the translations over the air
On this page
- Tutorial: localize a Flutter application step by step
- The ARB file
- Plurals and selects
- Escaping
- Fallback
- Choose and change the language
- Translate in Soluling Cloud
- Update the translations over the air
- Do and don't
- Typical localizability problems
- Troubleshooting
- Samples
- FAQ
Localize a Flutter application, step by step
Six steps take the Driving sample from an English-only application to a multilingual one.
Prepare: move the strings to app_en.arb
Add the localization packages, and turn on the code generation in pubspec.yaml. Then add l10n.yaml, which tells flutter gen-l10n where the ARB files are and which one is the template:
# pubspec.yaml
dependencies:
flutter_localizations:
sdk: flutter
intl: any
flutter:
generate: true
# l10n.yaml
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
Write the strings of the original language into lib/l10n/app_en.arb, each with a key, and describe them for the translator with an @ entry:
{
"@@locale": "en",
"drivingTime": "Driving Time",
"@drivingTime": {
"description": "The title of the page"
},
"result": "Driving time is {hours, plural, one{{hours} hour} other{{hours} hours}} {minutes, plural, one{{minutes} minute} other{{minutes} minutes}}",
"@result": {
"description": "The calculated driving time",
"placeholders": {
"hours": { "type": "int" },
"minutes": { "type": "int" }
}
}
}
Then give MaterialApp the delegates and locales of the generated class, and replace every text the user sees with its getter:
// lib\main.dart
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
onGenerateTitle: (context) => AppLocalizations.of(context)!.appTitle,
...
);
var l10n = AppLocalizations.of(context)!;
Text(l10n.drivingTime);
Text(l10n.result(hours, minutes));
From Flutter 3.47 on, Material is its own package, material_ui. Its widgets need the translations of material_ui's GlobalMaterialLocalizations, so list them yourself, with AppLocalizations.delegate, as the Driving sample does.
Add: create a Soluling project
Drag and drop lib/l10n/app_en.arb on Soluling. In the Select File Type dialog, select Flutter resource file: the file is also valid generic JSON, but only this parser reads the descriptions and the placeholders, and keeps the @ entries. Add the target languages, and click Finish. If the localized files already exist, such as app_fi.arb, Soluling imports their translations.
result takes the hours and the minutes as integers.Translate: texts, placeholders, and plural forms
Each message is a row in the grid, with its description as the comment. Placeholders such as {hours} are protected: the translator moves them but doesn't change them. A plural or select message opens in the pattern editor, which shows the forms that the target language needs: Finnish has one and other, Polish one, few, many, and other, and Japanese only other. Soluling writes the ICU syntax of each language back.
- Machine translation and translation memory. Click Use Machine Translation to translate with Amazon, Claude, DeepL, Google, Microsoft, ModernMT, or MyMemory, and reuse earlier translations from translation memory and terminology.
- External translators. Send a translation package, an Excel file, or XLIFF to a translator, and import the results back. See teamwork.
- Translators in a browser. Upload the project to Soluling Cloud and let translators work in Translate.
Check: find problems before release
Pseudo translation. Click Translate | Pseudo Translations | Fill, build, and run the application. Pseudo translations are longer and use accented characters, so texts that don't fit and strings still in the code (shown in plain English) stand out. See pseudo translation.
Pseudo-translated build
Validation. Soluling validates translations while you type and in batch (Validation | Validate Translations): missing translations, inconsistent placeholders and patterns, duplicate hotkeys, and more. See validation.
Validation
flutter gen-l10n also fails when a translation has a placeholder that the template doesn't have, so a broken translation never reaches a release build.
Build: create the localized ARB files
Choose Home | Build All. Soluling writes app_fi.arb, app_de.arb, and app_ja.arb into lib/l10n, with the descriptions and placeholders of the template. Then build the application as usual: flutter run or flutter build runs flutter gen-l10n, which compiles every ARB file into AppLocalizations and adds its language to supportedLocales. There is no code to change for a new language.
Update: translate only what changed
When you add or change strings in app_en.arb, click Home | Scan. Soluling keeps the existing translations, adds the new strings, and marks the rows whose original text changed. Translate the new rows, build, and build the application. Add the scan and build steps to your build pipeline with SoluMake or, with Soluling Cloud, SoluCLI.
The ARB file
An ARB file is JSON. Each message has a key and a text; an entry with the same key and an @ before it describes the message, and the @@locale entry tells the language of the file:
{
"@@locale": "en",
"hello": "Hello {name}!", // a placeholder
"@hello": {
"description": "A greeting", // the comment for the translator
"placeholders": {
"name": { "type": "String", "example": "Maija" }
}
},
"price": "Price: {amount}",
"@price": {
"placeholders": {
"amount": { "type": "double", "format": "currency", "optionalParameters": { "name": "EUR" } }
}
}
}
The type and format of a placeholder decide how its value is formatted: a number with NumberFormat, such as compact or currency, and a date with a DateFormat skeleton, such as yMd. Flutter formats them in the language of the application, so the translation keeps only the placeholder. The description is shown to the translator as the comment of the row, and the example as part of it.
Plurals and selects
Flutter's messages use the ICU syntax. A plural has a form for each plural category of the language, chosen by the count, and a select a form for each value, such as a gender:
"hours": "{count, plural, =0{no hours} one{{count} hour} other{{count} hours}}",
"driver": "{gender, select, female{She drove} male{He drove} other{They drove}}"
Soluling opens these in the pattern editor, which has a field for each form that the target language needs, and checks that the translation has the same structure. =0 is an exact value, and the other keys are the CLDR categories zero, one, two, few, many, and other. A message can have several plurals, such as the result of the Driving sample, which has one for the hours and one for the minutes.
Escaping
By default, { and } are always the braces of a placeholder or a plural. With use-escaping: true in l10n.yaml, text in single quotes is literal, such as '{name}', and two single quotes are a quote. Turn on the same option, Use escaping, in the options of the Flutter source in Soluling, so that it reads the messages the way Flutter does.
Fallback
flutter gen-l10n uses the template's text for a key that a localized ARB file doesn't have, so an untranslated string shows the original text. A device language without an ARB file of its own uses the closest supported locale: fi_FI uses app_fi.arb, and a language that the application doesn't have uses the first locale of supportedLocales.
Choose and change the language
By default, the application uses the language of the device. To let the user choose, keep the chosen locale in the state of the root widget, and give it to MaterialApp: the whole application is built again in the new language.
class _DrivingAppState extends State<DrivingApp> {
Locale? _locale; // null: the language of the device
@override
Widget build(BuildContext context) => MaterialApp(
locale: _locale,
...
home: DrivingPage(onLocaleChanged: (locale) => setState(() => _locale = locale)),
);
}
Translate in Soluling Cloud
With Soluling Cloud, translators use Translate in a browser. How you start depends on what you localize:
- Flutter ARB files (
.arb). On the dashboard, add a project, selectlib/l10n/app_en.arb, and choose Flutter resource file as the source type. Translators work in Translate; click Build localized files, and save the downloads tolib/l10nasapp_fi.arband so on, or let SoluCLI open a pull request with them. When the application changes, upload the newapp_en.arb; the existing translations are kept. No desktop step is needed.
To run the scan, upload, import, and build steps in your build pipeline, use SoluMake. See the Cloud tutorial and workflows.
Update the translations over the air
When you translate in Soluling Cloud and publish the project's files, the application can use them over the air: a translation fix or a new language reaches your users without a new release or an app store review. The translations that Flutter compiled stay in the application. soluling_ota downloads the published ARB files and uses their translations in place of the compiled ones; a message without a downloaded translation shows the compiled one.
flutter pub add soluling_ota
dart run soluling_ota:generate # after flutter gen-l10n: writes lib/l10n/ota_localizations.dart
// lib\main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await SolulingOta.init('1b6e2f0a-4c2d-4e9a-9f1a-0123456789ab');
runApp(const DrivingApp());
}
MaterialApp(
localizationsDelegates: [
OtaLocalizations.delegate, // in place of AppLocalizations.delegate
...GlobalMaterialLocalizations.delegates,
],
supportedLocales: OtaLocalizations.supportedLocales,
...
);
The generator reads l10n.yaml, the template ARB file, and the class that flutter gen-l10n generated, and writes a subclass of AppLocalizations whose getters and methods return the downloaded translation. The rest of the application doesn't change: AppLocalizations.of(context)!.drivingTime gives the downloaded text when there is one. Run the generator again when the ARB files change.
A downloaded file can't be compiled into the application, so the package formats its messages itself, with the same rules: the placeholders, the plural categories of the language, selects, and the number and date formats of the placeholders. SolulingOta.init uses the translations downloaded before at once, and checks for newer ones in the background once a day, so a fix is used on the next start. On the first start it waits for the first download for three seconds at most. SolulingOta.instance.update() checks now and uses newer translations at once, such as for a Check for new translations button.
Make the Soluling Cloud project from lib/l10n/app_en.arb, with Flutter resource file as the source type, and turn on Publish artifacts on the Integrations tab of the project's Manage page. OtaLocalizations.supportedLocales has the languages published in Soluling Cloud too, so a language added after the release can be chosen. A downloaded translation is not used when it can't be parsed, has a placeholder that the original doesn't have, or is the translation of a text that a later version of the application changed. The package works on Android, iOS, macOS, Windows, and Linux, but not on the web.
See the OTA sample (Samples\Flutter\Ota), the soluling_ota reference, and over-the-air updates for how publishing works.
Do and don't
Do
- Move every text the user sees to the ARB file, including the title from
onGenerateTitle. - Describe each message with a
description, and each placeholder with anexample. - Give each text a short, stable key.
- Write whole sentences with placeholders, and give the placeholders a type.
- Use ICU plurals and selects.
- Pseudo-translate before the first real translation.
Don't
- Build sentences from pieces in code.
- Use the English text as the key; the key changes when the text changes.
- Choose plural forms with
ifstatements; other languages have other rules. - Add
app_en.arbas a generic JSON file; the descriptions and placeholders are lost. - Format numbers and dates in code and pass them as text; give the placeholder a type and a format.
Typical localizability problems
See also internationalization and pseudo translation.
| Problem | How it shows | Find and fix |
|---|---|---|
| Hard-coded texts | Labels and hints in English in a pseudo-translated build | Move the text to app_en.arb, and use its getter. |
| Texts read too early | The old language stays after the user changes it | Read AppLocalizations.of(context) in build, not once in initState or a global variable. |
| Sentences built from pieces | Word order and plurals are wrong in the translation | Use one message with placeholders and an ICU plural. |
| Numbers formatted in code | English decimal points in the Finnish build | Pass the number, and give the placeholder a type and a format. |
| Fixed widths | Clipped or overflowing text in the pseudo or German build | Let the layout grow with the text, such as with Flexible and Wrap. |
Troubleshooting
| Symptom | Likely cause | What to do |
|---|---|---|
AppLocalizations is not found |
The code has not been generated | Set generate: true in pubspec.yaml, and run flutter gen-l10n. |
| The application stays in English | The device's language has no ARB file, or MaterialApp lacks the delegates |
Build the language's ARB file, and set localizationsDelegates and supportedLocales. |
| No MaterialLocalizations found | Flutter 3.47 or later, where Material is the material_ui package |
Add material_ui's GlobalMaterialLocalizations.delegates to the delegates. |
flutter gen-l10n reports a placeholder error |
A translation has a placeholder that the template doesn't have | Fix the translation in Soluling; validation shows the row. |
| Descriptions are missing in Soluling | The file was added as a generic JSON file | Create the project again with Flutter resource file. |
Samples
The samples are in Samples\Flutter of the Soluling installation and on GitHub:
- Ota: the driving time calculator of this guide, localized into Finnish, German, and Japanese, with a language selector, over-the-air translation updates from Soluling Cloud, and the Soluling project,
Ota.ntp. See Update the translations over the air. - Simple and Escape: ARB files with placeholders and plurals, and with escaping, and their Soluling projects.
See Flutter localization in the Desktop documentation for all the details.
FAQ
flutter gen-l10n. It is what the Flutter documentation recommends, the generated getters are checked at compile time, and the messages use the ICU syntax.supportedLocales on the next flutter gen-l10n, with no code changes. With over-the-air updates, a language published in Soluling Cloud reaches the installed applications too.Related
- Flutter localization - the full Desktop documentation
- Android localization - if you also have native Android applications
- iOS localization - if you also have native iOS applications
- Supported formats - everything else Soluling localizes