Sub topic 6
Connecting Localizations to `MaterialApp`
Assume you already know basic Dart, null safety, widgets, and MaterialApp. This chapter also assumes that flutter_localizations, intl, l10n.yaml, and at least one ARB file are configured. The gen-l10n tool has generated AppLocalizations.
The localization pipeline
Flutter loads translated resources through localization delegates. A delegate is an object that knows how to load messages for one locale. A locale is a language and optional region, such as en, es, or pt_BR.
The generated AppLocalizations.localizationsDelegates list contains your application delegate and the delegates used by Flutter's Material and Cupertino widgets. The generated supportedLocales list comes from the ARB files that gen-l10n found.
Connect both values to MaterialApp:
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';
void main() {
runApp(const TripApp());
}
class TripApp extends StatelessWidget {
const TripApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
onGenerateTitle: (context) {
return AppLocalizations.of(context)!.appTitle;
},
home: const TripScreen(),
);
}
}
class TripScreen extends StatelessWidget {
const TripScreen({super.key});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(
title: Text(l10n.appTitle),
),
body: Center(
child: Text(l10n.greeting('Ada')),
),
);
}
}localizationsDelegates tells Flutter how to load localized data. supportedLocales tells Flutter which locales the application can display. Both settings belong on the same MaterialApp that owns the localized widget tree.
Looking up messages with `BuildContext`
AppLocalizations.of(context) searches upward from the current widget for the localization data loaded by MaterialApp. Therefore, call it in a widget below MaterialApp, such as TripScreen.
The method returns AppLocalizations?, because a localization may not be available for a context or locale. The ! operator is reasonable when supportedLocales and the delegates are correctly configured. Use a null check instead when your widget can be used outside the application:
final l10n = AppLocalizations.of(context);
if (l10n == null) {
return const Text('Loading...');
}
return Text(l10n.appTitle);Do not create one AppLocalizations instance and pass it through the whole app. The lookup uses the current BuildContext, so it updates when the locale changes.
Localizing the application title
Use onGenerateTitle when the title itself is translated. Flutter calls this callback with a context that has access to the loaded localizations:
MaterialApp(
onGenerateTitle: (context) =>
AppLocalizations.of(context)!.appTitle,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: const TripScreen(),
)A fixed title: 'Trip Planner' would leave the title untranslated. On supported platforms, onGenerateTitle also lets Flutter obtain the localized title for system UI such as the task switcher.
> callout tip > Keep MaterialApp above every widget that calls AppLocalizations.of(context). A lookup made above MaterialApp cannot find its localization data.
Common wiring errors
- Importing the wrong generated file path after changing
l10n.yaml. - Supplying
supportedLocalesbut forgettinglocalizationsDelegates. - Calling
AppLocalizations.of(context)from a context aboveMaterialApp. - Using a locale in code that has no matching ARB file.
- Hard-coding an app-bar title while translating the page content.
Exercise
Why does a widget below MaterialApp usually use AppLocalizations.of(context)!, while onGenerateTitle uses the same lookup inside MaterialApp?
Show answer
Flutter provides a localization-aware context to onGenerateTitle, and widgets below MaterialApp inherit the loaded localization data through their BuildContext.