Localizations class

Defines the Locale for its child and the localized resources that the child depends on.

Defining localized resources

This following class is defined in terms of the Dart intl package. Using the intl package isn't required.
link
class MyLocalizations {
  MyLocalizations(this.locale);

  final Locale locale;

  static Future<MyLocalizations> load(Locale locale) {
    return initializeMessages(locale.toString())
      .then((void _) {
        return MyLocalizations(locale);
      });
  }

  static MyLocalizations of(BuildContext context) {
    return Localizations.of<MyLocalizations>(context, MyLocalizations)!;
  }

  String title() => Intl.message('<title>', name: 'title', locale: locale.toString());
  // ... more Intl.message() methods like title()
}
A class based on the intl package imports a generated message catalog that provides the initializeMessages() function and the per-locale backing store for Intl.message(). The message catalog is produced by an intl tool that analyzes the source code for classes that contain Intl.message() calls. In this case that would just be the MyLocalizations class.

One could choose another approach for loading localized resources and looking them up while still conforming to the structure of this example.

Loading localized resources

Localized resources are loaded by the list of LocalizationsDelegate delegates. Each delegate is essentially a factory for a collection of localized resources. There are multiple delegates because there are multiple sources for localizations within an app.

Delegates are typically simple subclasses of LocalizationsDelegate that override LocalizationsDelegate.load. For example a delegate for the MyLocalizations class defined above would be:

// continuing from previous example...
class _MyDelegate extends LocalizationsDelegate<MyLocalizations> {
  @override
  Future<MyLocalizations> load(Locale locale) => MyLocalizations.load(locale);

  @override
  bool isSupported(Locale locale) {
    // in a real implementation this would only return true for
    // locales that are definitely supported.
    return true;
  }

  @override
  bool shouldReload(_MyDelegate old) => false;
}

Each delegate can be viewed as a factory for objects that encapsulate a set of localized resources. These objects are retrieved with by runtime type with Localizations.of.

The WidgetsApp class creates a Localizations widget so most apps will not need to create one. The widget app's Localizations delegates can be initialized with WidgetsApp.localizationsDelegates. The MaterialApp class also provides a localizationsDelegates parameter that's just passed along to the WidgetsApp.

Obtaining localized resources for use in user interfaces

Apps should retrieve collections of localized resources with Localizations.of<MyLocalizations>(context, MyLocalizations), where MyLocalizations is an app specific class defines one function per resource. This is conventionally done by a static .of method on the custom localized resource class (MyLocalizations in the example above).

For example, using the MyLocalizations class defined above, one would lookup a localized title string like this:

// continuing from previous example...
MyLocalizations.of(context).title()

If Localizations were to be rebuilt with a new locale then the widget subtree that corresponds to BuildContext context would be rebuilt after the corresponding resources had been loaded.

This class is effectively an InheritedWidget. If it's rebuilt with a new locale or a different list of delegates or any of its delegates' LocalizationsDelegate.shouldReload() methods returns true, then widgets that have created a dependency by calling Localizations.of(context) will be rebuilt after the resources for the new locale have been loaded.

The Localizations widget also instantiates Directionality in order to support the appropriate Directionality.textDirection of the localized resources.

Inheritance

Constructors

Localizations({Key? key, required Locale locale, required List<LocalizationsDelegate> delegates, Widget? child})
Create a widget from which localizations (like translated strings) can be obtained.
Localizations.override({Key? key, required BuildContext context, Locale? locale, List<LocalizationsDelegate>? delegates, Widget? child})
Overrides the inherited Locale or LocalizationsDelegates for child.
factory

Properties

child Widget?
The widget below this widget in the tree.
final
delegates List<LocalizationsDelegate>
This list collectively defines the localized resources objects that can be retrieved with Localizations.of.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
locale Locale
The resources returned by Localizations.of will be specific to this locale.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<Localizations>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

localeOf(BuildContext context) Locale
The locale of the Localizations widget for the widget tree that corresponds to BuildContext context.
maybeLocaleOf(BuildContext context) Locale?
The locale of the Localizations widget for the widget tree that corresponds to BuildContext context.
of<T>(BuildContext context, Type type) → T?
Returns the localized resources object of the given type for the widget tree that corresponds to the given context.