text
stringlengths
1
372
defines the default text direction,
either left-to-right or right-to-left, for the widgets library.
more information about these app properties, the types they
depend on, and how internationalized flutter apps are typically
structured, is covered in this page.
<topic_end>
<topic_start>
overriding the locale
localizations.override is a factory constructor
for the localizations widget that allows for
(the typically rare) situation where a section of your application
needs to be localized to a different locale than the locale
configured for your device.
to observe this behavior, add a call to localizations.override
and a simple CalendarDatePicker:
<code_start>
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
title: text(widget.title),
),
body: center(
child: column(
mainAxisAlignment: MainAxisAlignment.center,
children: <widget>[
// add the following code
localizations.override(
context: context,
locale: const locale('es'),
// using a builder to get the correct BuildContext.
// alternatively, you can create a new widget and localizations.override
// will pass the updated BuildContext to the new widget.
child: builder(
builder: (context) {
// a toy example for an internationalized material widget.
return CalendarDatePicker(
initialDate: DateTime.now(),
firstDate: DateTime(1900),
lastDate: DateTime(2100),
onDateChanged: (value) {},
);
},
),
),
],
),
),
);
}
<code_end>
hot reload the app and the CalendarDatePicker
widget should re-render in spanish.
<topic_end>
<topic_start>
adding your own localized messages
after adding the flutter_localizations package,
you can configure localization.
to add localized text to your application,
complete the following instructions:
add the intl package as a dependency, pulling
in the version pinned by flutter_localizations:
open the pubspec.yaml file and enable the generate flag.
this flag is found in the flutter section in the pubspec file.
<code_start>
# the following section is specific to flutter.
flutter:
generate: true # add this line
<code_end>
add a new yaml file to the root directory of the flutter project.
name this file l10n.yaml and include following content:
<code_start>
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
<code_end>
this file configures the localization tool.
in this example, you’ve done the following:
in ${flutter_project}/lib/l10n,
add the app_en.arb template file. for example:
<code_start>
{
"helloworld": "hello world!",
"@helloworld": {
"description": "the conventional newborn programmer greeting"
}
}
<code_end>
add another bundle file called app_es.arb in the same directory.
in this file, add the spanish translation of the same message.
<code_start>
{
"helloworld": "¡hola mundo!"
}
<code_end>
now, run flutter pub get or flutter run and codegen takes place automatically.
you should find generated files in
${flutter_project}/.dart_tool/flutter_gen/gen_l10n.
alternatively, you can also run flutter gen-l10n to
generate the same files without running the app.
add the import statement on app_localizations.dart and