text stringlengths 1 474 |
|---|
);<code_end> |
The AppLocalizations class also provides auto-generated |
localizationsDelegates and supportedLocales lists. |
You can use these instead of providing them manually. |
<code_start>const MaterialApp( |
title: 'Localizations Sample App', |
localizationsDelegates: AppLocalizations.localizationsDelegates, |
supportedLocales: AppLocalizations.supportedLocales, |
);<code_end> |
Once the Material app has started, |
you can use AppLocalizations anywhere in your app: |
<code_start>appBar: AppBar( |
// The [AppBar] title text should update its message |
// according to the system locale of the target platform. |
// Switching between English and Spanish locales should |
// cause this text to update. |
title: Text(AppLocalizations.of(context)!.helloWorld), |
),<code_end> |
info Note |
The Material app has to actually be started to initialize |
AppLocalizations. If the app hasn’t yet started, |
AppLocalizations.of(context)!.helloWorld causes a |
null exception.This code generates a Text widget that displays “Hello World!” |
if the target device’s locale is set to English, |
and “¡Hola Mundo!” if the target device’s locale is set |
to Spanish. In the arb files, |
the key of each entry is used as the method name of the getter, |
while the value of that entry contains the localized message.The gen_l10n_example uses this tool.To localize your device app description, |
pass the localized string to |
MaterialApp.onGenerateTitle: |
<code_start>return MaterialApp( |
onGenerateTitle: (context) => DemoLocalizations.of(context).title,<code_end> |
<topic_end> |
<topic_start> |
Placeholders, plurals, and selects |
lightbulb Tip |
When using VS Code, add the arb-editor extension. |
This extension adds syntax highlighting, snippets, |
diagnostics, and quick fixes to help edit .arb template files.You can also include application values in a message with |
special syntax that uses a placeholder to generate a method |
instead of a getter. |
A placeholder, which must be a valid Dart identifier name, |
becomes a positional parameter in the generated method in the |
AppLocalizations code. Define a placeholder name by wrapping |
it in curly braces as follows:Define each placeholder in the placeholders object |
in the app’s .arb file. For example, |
to define a hello message with a userName parameter, |
add the following to lib/l10n/app_en.arb: |
<code_start>"hello": "Hello {userName}", |
"@hello": { |
"description": "A message with a single parameter", |
"placeholders": { |
"userName": { |
"type": "String", |
"example": "Bob" |
} |
} |
}<code_end> |
This code snippet adds a hello method call to |
the AppLocalizations.of(context) object, |
and the method accepts a parameter of type String; |
the hello method returns a string. |
Regenerate the AppLocalizations file.Replace the code passed into Builder with the following: |
<code_start>// Examples of internationalized strings. |
return Column( |
children: <Widget>[ |
// Returns 'Hello John' |
Text(AppLocalizations.of(context)!.hello('John')), |
], |
);<code_end> |
You can also use numerical placeholders to specify multiple values. |
Different languages have different ways to pluralize words. |
The syntax also supports specifying how a word should be pluralized. |
A pluralized message must include a num parameter indicating |
how to pluralize the word in different situations. |
English, for example, pluralizes “person” to “people”, |
but that doesn’t go far enough. |
The message0 plural might be “no people” or “zero people”. |
The messageFew plural might be |
“several people”, “some people”, or “a few people”. |
The messageMany plural might |
be “most people” or “many people”, or “a crowd”. |
Only the more general messageOther field is required. |
The following example shows what options are available:The previous expression is replaced by the message variation |
(message0, message1, …) corresponding to the value |
of the countPlaceholder. |
Only the messageOther field is required.The following example defines a message that pluralizes |
the word, “wombat”: |
<code_start>"nWombats": "{count, plural, =0{no wombats} =1{1 wombat} other{{count} wombats}}", |
"@nWombats": { |
"description": "A plural message", |
"placeholders": { |
"count": { |
"type": "num", |
"format": "compact" |
} |
} |
}<code_end> |
Use a plural method by passing in the count parameter: |
<code_start>// Examples of internationalized strings. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.