text
stringlengths
1
372
Locale.fromSubtags(
languageCode: 'zh',
scriptCode: 'hant'), // generic traditional chinese 'zh_hant'
Locale.fromSubtags(
languageCode: 'zh',
scriptCode: 'hans',
countryCode: 'cn'), // 'zh_hans_cn'
Locale.fromSubtags(
languageCode: 'zh',
scriptCode: 'hant',
countryCode: 'tw'), // 'zh_hant_tw'
Locale.fromSubtags(
languageCode: 'zh',
scriptCode: 'hant',
countryCode: 'hk'), // 'zh_hant_hk'
],
<code_end>
this explicit full definition ensures that your app can
distinguish between and provide the fully nuanced localized
content to all combinations of these country codes.
if a user’s preferred locale isn’t specified,
flutter selects the closest match,
which likely contains differences to what the user expects.
flutter only resolves to locales defined in supportedLocales
and provides scriptCode-differentiated localized
content for commonly used languages.
see localizations for information on how the supported
locales and the preferred locales are resolved.
although chinese is a primary example,
other languages like french (fr_fr, fr_CA)
should also be fully differentiated for more nuanced localization.
<topic_end>
<topic_start>
tracking the locale: the locale class and the localizations widget
the locale class identifies the user’s language.
mobile devices support setting the locale for all applications,
usually using a system settings menu.
internationalized apps respond by displaying values that are
locale-specific. for example, if the user switches the device’s locale
from english to french, then a text widget that originally
displayed “hello world” would be rebuilt with “bonjour le monde”.
the localizations widget defines the locale
for its child and the localized resources that the child depends on.
the WidgetsApp widget creates a localizations widget
and rebuilds it if the system’s locale changes.
you can always look up an app’s current locale with
Localizations.localeOf():
<code_start>
locale myLocale = Localizations.localeOf(context);
<code_end>
<topic_end>
<topic_start>
specifying the app’s supported­Locales parameter
although the flutter_localizations library currently supports
115 languages and language variants, only english language translations
are available by default. it’s up to the developer to decide exactly
which languages to support.
the MaterialApp supportedLocales
parameter limits locale changes. when the user changes the locale
setting on their device, the app’s localizations widget only
follows suit if the new locale is a member of this list.
if an exact match for the device locale isn’t found,
then the first supported locale with a matching languageCode
is used. if that fails, then the first element of the
supportedLocales list is used.
an app that wants to use a different “locale resolution”
method can provide a localeResolutionCallback.
for example, to have your app unconditionally accept
whatever locale the user selects:
<code_start>
MaterialApp(
localeResolutionCallback: (
locale,
supportedLocales,
) {
return locale;
},
);
<code_end>
<topic_end>
<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,