Sub topic 5
Generating the `AppLocalizations` Class
This chapter assumes that you already know basic Dart, can edit YAML and JSON, and have added flutter_localizations, intl, and generate: true to the Flutter project. It also assumes that your project has a template ARB file and an l10n.yaml file.
Flutter's gen-l10n tool reads ARB files and generates Dart code. The generated code contains an AppLocalizations class. Your widgets use this class instead of reading JSON files directly.
How code generation works
The process has three inputs:
l10n.yamltells Flutter where the ARB files are and what to generate.- The template ARB file defines the message keys and their metadata.
- Other ARB files provide translations for supported locales.
The output is a Dart file such as lib/l10n/app_localizations.dart.
A configuration with synthetic-package: false writes the generated file into your project:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: falseWith this configuration, Flutter uses lib/l10n/app_en.arb as the template and writes lib/l10n/app_localizations.dart.
Run generation explicitly from the project root:
flutter pub get
flutter gen-l10nWhen generate: true is present in pubspec.yaml, Flutter can also run localization generation during commands such as flutter run and flutter build.
What the generated class contains
For a template containing appTitle, greeting, and tripCount, the generated class exposes members similar to these:
| ARB entry | Generated Dart API |
|---|---|
| `"appTitle": "Trip Planner"` | `l10n.appTitle` |
| `"greeting": "Hello, {name}!"` | `l10n.greeting('Ada')` |
| `"tripCount": "{count, plural, ...}"` | `l10n.tripCount(3)` |
A message without placeholders becomes a getter. A message with a placeholder becomes a method. The placeholder metadata determines the parameter type. For example, a DateTime placeholder produces a method that accepts a DateTime.
The class also provides localization information used by MaterialApp:
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(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: Builder(
builder: (context) {
final l10n = AppLocalizations.of(context)!;
return Text(l10n.appTitle);
},
),
),
);
}
}AppLocalizations.of(context) looks up the localization object associated with the current widget tree. The generated delegates load the correct messages for the active locale.
Common generation errors
Generation fails when the template file is missing, the ARB files contain invalid JSON, or a message uses inconsistent placeholder metadata. Check these items first:
- The
arb-dirpath is relative to the project root. template-arb-fileexactly matches the template filename.- Every ARB file contains valid JSON.
- Placeholder names match inside the message and its metadata.
- Generated files are not edited by hand.
Exercise
What does Flutter generate for an ARB message named "greeting" with a String placeholder named "name"?
Show answer
It generates a method similar to greeting(String name), which a widget can call as l10n.greeting('Ada').