• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Material and Cupertino Libraries Localizations
2
3The `.arb` files in this directory contain localized values (primarily
4strings) used by the Material and Cupertino libraries.  The
5`generated_material_localizations.dart` and
6`generated_cupertino_localizations.dart` files combine all of the
7localizations into a single Map that is linked with the rest of
8flutter_localizations package.
9
10If you're looking for information about internationalizing Flutter
11apps in general, see the
12[Internationalizing Flutter Apps](https://flutter.dev/tutorials/internationalization/) tutorial.
13
14
15### Translations for one locale: .arb files
16
17The Material and Cupertino libraries use
18[Application Resource Bundle](https://code.google.com/p/arb/wiki/ApplicationResourceBundleSpecification)
19files, which have a `.arb` extension, to store localized translations
20of messages, format strings, and other values. This format is also
21used by the Dart [intl](https://pub.dev/packages/intl)
22package and it is supported by the
23[Google Translators Toolkit](https://translate.google.com/toolkit).
24
25The Material and Cupertino libraries only depend on a small subset
26of the ARB format. Each .arb file contains a single JSON table that
27maps from resource IDs to localized values.
28
29Filenames contain the locale that the values have been translated
30for. For example `material_de.arb` contains German translations, and
31`material_ar.arb` contains Arabic translations. Files that contain
32regional translations have names that include the locale's regional
33suffix. For example `material_en_GB.arb` contains additional English
34translations that are specific to Great Britain.
35
36There is one language-specific .arb file for each supported locale. If
37an additional file with a regional suffix is present, the regional
38localizations are automatically merged with the language-specific ones.
39
40The JSON table's keys, called resource IDs, are valid Dart variable
41names. They correspond to methods from the `MaterialLocalizations` or
42`CupertinoLocalizations` classes. For example:
43
44```dart
45Widget build(BuildContext context) {
46  return FlatButton(
47    child: Text(
48      MaterialLocalizations.of(context).cancelButtonLabel,
49    ),
50  );
51}
52```
53
54This widget build method creates a button whose label is the local
55translation of "CANCEL" which is defined for the `cancelButtonLabel`
56resource ID.
57
58Each of the language-specific .arb files contains an entry for
59`cancelButtonLabel`. They're all represented by the `Map` in the
60generated `localizations.dart` file. The Map is used by the
61MaterialLocalizations class.
62
63
64### material_en.arb and cupertino_en.arb Define all of the resource IDs
65
66All of the `material_*.arb` files whose names do not include a regional
67suffix contain translations for the same set of resource IDs as
68`material_en.arb`.
69
70Similarly all of the `cupertino_*.arb` files whose names do not include
71a regional suffix contain translations for the same set of resource IDs
72as `cupertino_en.arb`.
73
74For each resource ID defined for English, there is an additional resource
75with an '@' prefix. These '@' resources are not used by the generated
76Dart code at run time, they just exist to inform translators about how
77the value will be used, and to inform the code generator about what code
78to write.
79
80```dart
81"cancelButtonLabel": "CANCEL",
82"@cancelButtonLabel": {
83  "description": "The label for cancel buttons and menu items.",
84  "type": "text"
85},
86```
87
88
89### Values with Parameters, Plurals
90
91A few of material translations contain `$variable` tokens. The
92Material and Cupertino libraries replace these tokens with values at
93run-time. For example:
94
95```dart
96"aboutListTileTitle": "About $applicationName",
97```
98
99The value for this resource ID is retrieved with a parameterized
100method instead of a simple getter:
101
102```dart
103MaterialLocalizations.of(context).aboutListTileTitle(yourAppTitle)
104```
105
106The names of the `$variable` tokens must match the names of the
107`MaterialLocalizations` method parameters.
108
109
110Plurals are handled similarly, with a lookup method that includes a
111quantity parameter. For example `selectedRowCountTitle` returns a
112string like "1 item selected" or "no items selected".
113
114```dart
115MaterialLocalizations.of(context).selectedRowCountTitle(yourRowCount)
116```
117
118Plural translations can be provided for several quantities: 0, 1, 2,
119"few", "many", "other". The variations are identified by a resource ID
120suffix which must be one of "Zero", "One", "Two", "Few", "Many",
121"Other". The "Other" variation is used when none of the other
122quantities apply. All plural resources must include a resource with
123the "Other" suffix. For example the English translations
124('material_en.arb') for `selectedRowCountTitle` are:
125
126```dart
127"selectedRowCountTitleZero": "No items selected",
128"selectedRowCountTitleOne": "1 item selected",
129"selectedRowCountTitleOther": "$selectedRowCount items selected",
130```
131
132When defining new resources that handle pluralizations, the "One" and
133the "Other" forms must, at minimum, always be defined in the source
134English ARB files.
135
136### scriptCategory and timeOfDayFormat for Material library
137
138In `material_en.arb`, the values of these resource IDs are not
139translations, they're keywords that help define an app's text theme
140and time picker layout respectively.
141
142The value of `timeOfDayFormat` defines how a time picker displayed by
143[showTimePicker()](https://docs.flutter.io/flutter/material/showTimePicker.html)
144formats and lays out its time controls. The value of `timeOfDayFormat`
145must be a string that matches one of the formats defined by
146<https://docs.flutter.io/flutter/material/TimeOfDayFormat-class.html>.
147It is converted to an enum value because the `material_en.arb` file
148has this value labeled as `"x-flutter-type": "icuShortTimePattern"`.
149
150The value of `scriptCategory` is based on the
151[Language categories reference](https://material.io/design/typography/language-support.html#language-categories-reference)
152section in the Material spec. The Material theme uses the
153`scriptCategory` value to lookup a localized version of the default
154`TextTheme`, see
155[Typography.geometryThemeFor](https://docs.flutter.io/flutter/material/Typography/geometryThemeFor.html).
156
157
158### 'generated_*_localizations.dart': all of the localizations as a Map
159
160If you look at the comment at the top of the `generated_material_localizations.dart`
161and `generated_cupertino_localizations.dart` files, you'll
162see that it was manually generated using a `dev/tools/localizations`
163app called `gen_localizations`.
164
165You can see what that script would generate by running this command:
166
167```dart
168dart dev/tools/localizations/gen_localizations.dart packages/flutter_localizations/lib/src/l10n material
169```
170
171The gen_localizations app just combines the contents of all of the
172.arb files into a single `Map` per library that has entries for each .arb
173file's locale. The `MaterialLocalizations` and `CupertinoLocalizations`
174class implementations use these Maps to implement the methods that lookup localized resource values.
175
176The gen_localizations app must be run by hand after .arb files have
177been updated. The app's first parameter is the path to this directory,
178the second is the file name prefix (the file name less the locale
179suffix) for the .arb files in this directory.
180
181To in-place update the generated localizations file using the default
182values, you can just run:
183
184```dart
185dart dev/tools/localizations/gen_localizations.dart --overwrite
186```
187
188
189### Special handling for the Kannada (kn) translations
190
191Originally, the cupertino_kn.arb and material_kn.arb files contained unicode
192characters that can cause current versions of Emacs on Linux to crash. There is
193more information here: https://github.com/flutter/flutter/issues/36704.
194
195Rather than risking developers' editor sessions, the strings in these arb files
196(and the code generated for them) have been encoded using the appropriate
197escapes for JSON and Dart. The JSON format arb files were rewritten with
198dev/tools/localization/encode_kn_arb_files.dart. The localizations code
199generator uses generateEncodedString() from dev/tools/localization/localizations_utils.
200
201
202### Translations Status, Reporting Errors
203
204The translations (the `.arb` files) in this directory are based on the
205English translations in `material_en.arb` and `cupertino_en.arb`.
206Google contributes translations for all the languages supported by
207this package. (Googlers, for more details see <go/flutter-l10n>.)
208
209If you have feedback about the translations please
210[file an issue on the Flutter github repo](https://github.com/flutter/flutter/issues/new?template=BUG.md).
211
212
213### See Also
214
215The [Internationalizing Flutter Apps](https://flutter.dev/tutorials/internationalization/)
216tutorial describes how to use the internationalization APIs in an
217ordinary Flutter app.
218
219[Application Resource Bundle](https://code.google.com/p/arb/wiki/ApplicationResourceBundleSpecification)
220covers the `.arb` file format used to store localized translations
221of messages, format strings, and other values.
222
223The Dart [intl](https://pub.dev/packages/intl)
224package supports internationalization.
225