text
stringlengths
1
474
<topic_start>
Configuring the l10n.yaml file
The l10n.yaml file allows you to configure the gen-l10n tool
to specify the following:For a full list of options, either run flutter gen-l10n --help
at the command line or refer to the following table:<topic_end>
<topic_start>
How internationalization in Flutter works
This section covers the technical details of how localizations work
in Flutter. If you’re planning on supporting your own set of localized
messages, the following content would be helpful.
Otherwise, you can skip this section.<topic_end>
<topic_start>
Loading and retrieving localized values
The Localizations widget is used to load and
look up objects that contain collections of localized values.
Apps refer to these objects with Localizations.of(context,type).
If the device’s locale changes,
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