text stringlengths 1 474 |
|---|
strings and other values for the Material Components |
library. GlobalWidgetsLocalizations.delegate |
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 |
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(), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.