text stringlengths 1 372 |
|---|
AppLocalizations.delegate |
in your call to the constructor for MaterialApp: |
<code_start> |
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; |
<code_end> |
<code_start> |
return const MaterialApp( |
title: 'localizations sample app', |
localizationsDelegates: [ |
AppLocalizations.delegate, // add this line |
GlobalMaterialLocalizations.delegate, |
GlobalWidgetsLocalizations.delegate, |
GlobalCupertinoLocalizations.delegate, |
], |
supportedLocales: [ |
locale('en'), // english |
locale('es'), // spanish |
], |
home: MyHomePage(), |
); |
<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( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.