Localizations class
Defines theLocale for itschild and the localized resources that thechild depends on.
Defining localized resources
intl package. Using theintlpackage 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()}intl package imports a generated message catalog that providestheinitializeMessages() function and the per-locale backing store forIntl.message().The message catalog is produced by anintl tool that analyzes the source code forclasses that containIntl.message() calls. In this case that would just be theMyLocalizations class.One could choose another approach for loading localized resources and looking them up whilestill conforming to the structure of this example.
Loading localized resources
Localized resources are loaded by the list ofLocalizationsDelegatedelegates. Each delegate is essentially a factory for a collectionof localized resources. There are multiple delegates because there aremultiple sources for localizations within an app.
Delegates are typically simple subclasses ofLocalizationsDelegate thatoverrideLocalizationsDelegate.load. For example a delegate for theMyLocalizations 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 setof localized resources. These objects are retrieved withby runtime type withLocalizations.of.
TheWidgetsApp class creates aLocalizations widget so most appswill not need to create one. The widget app'sLocalizations delegates canbe initialized withWidgetsApp.localizationsDelegates. TheMaterialAppclass also provides alocalizationsDelegates parameter that's justpassed along to theWidgetsApp.
Obtaining localized resources for use in user interfaces
Apps should retrieve collections of localized resources withLocalizations.of<MyLocalizations>(context, MyLocalizations),where MyLocalizations is an app specific class defines one function perresource. This is conventionally done by a static.of method on thecustom localized resource class (MyLocalizations in the example above).
For example, using theMyLocalizations class defined above, one wouldlookup a localized title string like this:
// continuing from previous example...MyLocalizations.of(context).title()IfLocalizations were to be rebuilt with a newlocale thenthe widget subtree that corresponds toBuildContextcontext wouldbe rebuilt after the corresponding resources had been loaded.
This class is effectively anInheritedWidget. If it's rebuilt witha newlocale or a different list of delegates or any of itsdelegates'LocalizationsDelegate.shouldReload() methods returns true,then widgets that have created a dependency by callingLocalizations.of(context) will be rebuilt after the resourcesfor the new locale have been loaded.
TheLocalizations widget also instantiatesDirectionality in order tosupport the appropriateDirectionality.textDirection of the localizedresources.
- Inheritance
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- Localizations
Constructors
- Localizations({Key?key,requiredLocalelocale,requiredList<
LocalizationsDelegate> delegates,Widget?child,boolisApplicationLevel =false}) - Create a widget from which localizations (like translated strings) can be obtained.
- Localizations.override({Key?key,requiredBuildContextcontext,Locale?locale,List<
LocalizationsDelegate> ?delegates,Widget?child}) - Overrides the inheritedLocale orLocalizationsDelegates 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 canbe retrieved withLocalizations.of.final
- hashCode→int
- The hash code for this object.no setterinherited
- isApplicationLevel→bool
- Whether this is the main localizations widget that represents the app'slocale.final
- key→Key?
- Controls how one widget replaces another widget in the tree.finalinherited
- locale→Locale
- The resources returned byLocalizations.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 aStatefulElement 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 ofDiagnosticsNode objects describing this node'schildren.inherited
- debugFillProperties(
DiagnosticPropertiesBuilderproperties)→ void - Add additional properties associated with the node.override
- noSuchMethod(
Invocationinvocation)→ 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 debuggingtools and byDiagnosticsNode.toStringDeep.inherited
- toString(
{DiagnosticLevelminLevel =DiagnosticLevel.info})→String - A string representation of this object.inherited
- toStringDeep(
{StringprefixLineOne ='',String?prefixOtherLines,DiagnosticLevelminLevel =DiagnosticLevel.debug,intwrapWidth =65})→String - Returns a string representation of this node and its descendants.inherited
- toStringShallow(
{Stringjoiner =', ',DiagnosticLevelminLevel =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 ==(
Objectother)→bool - The equality operator.inherited
Static Methods
- localeOf(
BuildContextcontext)→Locale - The locale of the Localizations widget for the widget tree thatcorresponds toBuildContext
context. - maybeLocaleOf(
BuildContextcontext)→Locale? - The locale of the Localizations widget for the widget tree thatcorresponds toBuildContext
context. - of<
T> (BuildContextcontext,Typetype)→ T? - Returns the localized resources object of the given
typefor the widgettree that corresponds to the givencontext.