localizationsDelegates property Null safety
The delegates for this app's Localizations widget.
The delegates collectively define all of the localized resources for this application's Localizations widget.
Internationalized apps that require translations for one of the locales listed in GlobalMaterialLocalizations should specify this parameter and list the supportedLocales that the application can handle.
import 'package:flutter_localizations/flutter_localizations.dart';
MaterialApp(
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'), // English
const Locale('he', 'IL'), // Hebrew
// ... other locales the app supports
],
// ...
)
Adding localizations for a new locale
The information that follows applies to the unusual case of an app adding translations for a language not already supported by GlobalMaterialLocalizations.
Delegates that produce WidgetsLocalizations and MaterialLocalizations
are included automatically. Apps can provide their own versions of these
localizations by creating implementations of
LocalizationsDelegate<WidgetsLocalizations>
or
LocalizationsDelegate<MaterialLocalizations>
whose load methods return
custom versions of WidgetsLocalizations or MaterialLocalizations.
For example: to add support to MaterialLocalizations for a
locale it doesn't already support, say const Locale('foo', 'BR')
,
one could just extend DefaultMaterialLocalizations:
class FooLocalizations extends DefaultMaterialLocalizations {
FooLocalizations(Locale locale) : super(locale);
@override
String get okButtonLabel {
if (locale == const Locale('foo', 'BR'))
return 'foo';
return super.okButtonLabel;
}
}
A FooLocalizationsDelegate
is essentially just a method that constructs
a FooLocalizations
object. We return a SynchronousFuture here because
no asynchronous work takes place upon "loading" the localizations object.
class FooLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
const FooLocalizationsDelegate();
@override
Future<FooLocalizations> load(Locale locale) {
return SynchronousFuture(FooLocalizations(locale));
}
@override
bool shouldReload(FooLocalizationsDelegate old) => false;
}
Constructing a MaterialApp with a FooLocalizationsDelegate
overrides
the automatically included delegate for MaterialLocalizations because
only the first delegate of each LocalizationsDelegate.type is used and
the automatically included delegates are added to the end of the app's
localizationsDelegates list.
MaterialApp(
localizationsDelegates: [
const FooLocalizationsDelegate(),
],
// ...
)
See also:
- supportedLocales, which must be specified along with localizationsDelegates.
- GlobalMaterialLocalizations, a localizationsDelegates value which provides material localizations for many languages.
- The Flutter Internationalization Tutorial, flutter.dev/tutorials/internationalization/.
Implementation
final Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates;