text stringlengths 1 372 |
|---|
the localizations widget automatically loads values for |
the new locale and then rebuilds widgets that used it. |
this happens because localizations works like an |
InheritedWidget. |
when a build function refers to an inherited widget, |
an implicit dependency on the inherited widget is created. |
when an inherited widget changes |
(when the localizations widget’s locale changes), |
its dependent contexts are rebuilt. |
localized values are loaded by the localizations widget’s |
list of LocalizationsDelegates. |
each delegate must define an asynchronous load() |
method that produces an object that encapsulates a |
collection of localized values. |
typically these objects define one method per localized value. |
in a large app, different modules or packages might be bundled with |
their own localizations. that’s why the localizations widget |
manages a table of objects, one per LocalizationsDelegate. |
to retrieve the object produced by one of the LocalizationsDelegate’s |
load methods, specify a BuildContext and the object’s type. |
for example, |
the localized strings for the material components widgets |
are defined by the MaterialLocalizations class. |
instances of this class are created by a LocalizationDelegate |
provided by the MaterialApp class. |
they can be retrieved with localizations.of(): |
this particular localizations.of() expression is used frequently, |
so the MaterialLocalizations class provides a convenient shorthand: |
<topic_end> |
<topic_start> |
defining a class for the app’s localized resources |
putting together an internationalized flutter app usually |
starts with the class that encapsulates the app’s localized values. |
the example that follows is typical of such classes. |
complete source code for the intl_example for this app. |
this example is based on the APIs and tools provided by the |
intl package. the an alternative class for the app’s |
localized resources section |
describes an example that doesn’t depend on the intl package. |
the DemoLocalizations class |
(defined in the following code snippet) |
contains the app’s strings (just one for the example) |
translated into the locales that the app supports. |
it uses the initializeMessages() function |
generated by dart’s intl package, |
intl.message(), to look them up. |
<code_start> |
class DemoLocalizations { |
DemoLocalizations(this.localeName); |
static Future<DemoLocalizations> load(Locale locale) { |
final string name = |
locale.countryCode == null || locale.countryCode!.isEmpty |
? locale.languageCode |
: locale.toString(); |
final string localeName = Intl.canonicalizedLocale(name); |
return initializeMessages(localeName).then((_) { |
return DemoLocalizations(localeName); |
}); |
} |
static DemoLocalizations of(BuildContext context) { |
return Localizations.of<DemoLocalizations>(context, DemoLocalizations)!; |
} |
final string localeName; |
string get title { |
return intl.message( |
'hello world', |
name: 'title', |
desc: 'title for the demo application', |
locale: localeName, |
); |
} |
} |
<code_end> |
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 DemoLocalizations class. |
<topic_end> |
<topic_start> |
adding support for a new language |
an app that needs to support a language that’s not included in |
GlobalMaterialLocalizations has to do some extra work: |
it must provide about 70 translations (“localizations”) |
for words or phrases and the date patterns and symbols for the |
locale. |
see the following for an example of how to add |
support for the norwegian nynorsk language. |
a new GlobalMaterialLocalizations subclass defines the |
localizations that the material library depends on. |
a new LocalizationsDelegate subclass, which serves |
as factory for the GlobalMaterialLocalizations subclass, |
must also be defined. |
here’s the source code for the complete add_language example, |
minus the actual nynorsk translations. |
the locale-specific GlobalMaterialLocalizations subclass |
is called NnMaterialLocalizations, |
and the LocalizationsDelegate subclass is |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.