Add `flutter_localizations` and `intl` dependencies, enable "generate true" in `pubspec.yaml`, and create an `l10n.yaml` configuration file. Use when initializing localization support for a new Flutter project.
flutter_localizations and intl packages. The standard approach uses App Resource Bundle (.arb) files to define localized strings, which are then compiled into a generated AppLocalizations class for type-safe access within the widget tree.pubspec.yaml.generate flag.l10n.yaml configuration file.MaterialApp or CupertinoApp.flutter pub add flutter_localizations --sdk=flutter
flutter pub add intl:any
pubspec.yaml includes the following under dependencies:dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: any
pubspec.yaml and enable the generate flag within the flutter section to automate localization tasks:flutter:
generate: true
l10n.yaml in the root directory of the Flutter project. Define the input directory, template file, and output file:arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: true
flutter_localizations library in your main.dart. Inject the delegates and supported locales into your MaterialApp or CupertinoApp.import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; // Adjust path if synthetic-package is false
// ... inside build method
return MaterialApp(
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en'), // English
Locale('es'), // Spanish
],
home: const MyHomePage(),
);
lib/l10n/app_en.arb). Include a description for context..arb files and update the values.{
"helloWorld": "Hello World!",
"@helloWorld": {
"description": "The conventional newborn programmer greeting"
}
}
app_es.arb):{
"helloWorld": "¡Hola Mundo!"
}
flutter pub get
flutter pub get.AppLocalizations.of(context). Ensure the widget calling this is a descendant of MaterialApp.Text(AppLocalizations.of(context)!.helloWorld)
"hello": "Hello {userName}",
"@hello": {
"description": "A message with a single parameter",
"placeholders": {
"userName": {
"type": "String",
"example": "Bob"
}
}
}
plural syntax to handle quantity-based string variations. The other case is mandatory."nWombats": "{count, plural, =0{no wombats} =1{1 wombat} other{{count} wombats}}",
"@nWombats": {
"description": "A plural message",
"placeholders": {
"count": {
"type": "num",
"format": "compact"
}
}
}
select syntax for conditional strings, such as gendered text."pronoun": "{gender, select, male{he} female{she} other{they}}",
"@pronoun": {
"description": "A gendered message",
"placeholders": {
"gender": {
"type": "String"
}
}
}
l10n.yamlarb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: true
use-escaping: true
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class GreetingWidget extends StatelessWidget {
final String userName;
final int notificationCount;
const GreetingWidget({
super.key,
required this.userName,
required this.notificationCount,
});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Column(
children: [
Text(l10n.hello(userName)),
Text(l10n.nWombats(notificationCount)),
],
);
}
}