text
stringlengths
1
474
including supported locales, in an Info.plist file
that is built into the application bundle.
To configure the locales supported by your app,
use the following instructions:Open your project’s ios/Runner.xcworkspace Xcode file.In the Project Navigator, open the Info.plist file
under the Runner project’s Runner folder.Select the Information Property List item.
Then select Add Item from the Editor menu,
and select Localizations from the pop-up menu.Select and expand the newly-created Localizations item.
For each locale your application supports,
add a new item and select the locale you wish to add
from the pop-up menu in the Value field.
This list should be consistent with the languages listed
in the supportedLocales parameter.Once all supported locales have been added, save the file.<topic_end>
<topic_start>
Advanced topics for further customization
This section covers additional ways to customize a
localized Flutter application.<topic_end>
<topic_start>
Advanced locale definition
Some languages with multiple variants require more than just a
language code to properly differentiate.For example, fully differentiating all variants of
Chinese requires specifying the language code, script code,
and country code. This is due to the existence
of simplified and traditional script, as well as regional
differences in the way characters are written within the same script type.In order to fully express every variant of Chinese for the
country codes CN, TW, and HK, the list of supported
locales should include:
<code_start>supportedLocales: [
Locale.fromSubtags(languageCode: 'zh'), // generic Chinese 'zh'
Locale.fromSubtags(
languageCode: 'zh',
scriptCode: 'Hans'), // generic simplified Chinese 'zh_Hans'
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>