Localizations class

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

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 below would be:

class _MyDelegate extends LocalizationsDelegate<MyLocalizations> {
  @override
  Future<MyLocalizations> load(Locale locale) => MyLocalizations.load(locale);

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

Each delegate can be viewed as a factory for objects that encapsulate a 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.

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 MyLocalizations class.

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

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.

This following class is defined in terms of the Dart intl package. Using the intl package isn't required.
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.

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
locale Locale
The resources returned by Localizations.of will be specific to this locale.
final
hashCode int
The hash code for this object. [...]
read-only, inherited
key Key
Controls how one widget replaces another widget in the tree. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

createState() → _LocalizationsState
Creates the mutable state for this widget at a given location in the tree. [...]
override
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
override
createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree. [...]
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children. [...]
@protected, inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent 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 toStringDeep. [...]
inherited
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug }) String
Returns a string representation of this object.
inherited
toStringDeep({String prefixLineOne: '', String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug }) 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 ==(dynamic other) bool
The equality operator. [...]
inherited

Static Methods

localeOf(BuildContext context, { bool nullOk: false }) 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. [...]