Skip to content

Commit 39ffce3

Browse files
author
Shi-Hao Hong
authored
Update stocks example to use l10n.yaml workflow (flutter#58121)
1 parent 7cdf26d commit 39ffce3

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Options used by the localizations tool
2+
## `arb-dir` sets the input directory. The output directory will match
3+
## the input directory if the output directory is not set.
4+
arb-dir: lib/i18n
5+
## `template-arb-file` describes the template arb file that the tool
6+
## will use to check and validate the remaining arb files when
7+
## generating Flutter's localization files.
8+
template-arb-file: stocks_en.arb
9+
## `output-localization-file` is the name of the generated file.
10+
output-localization-file: stock_strings.dart
11+
## `output-class` is the name of the localizations class your
12+
## Flutter application will use. The file will need to be
13+
## imported throughout your application.
14+
output-class: StockStrings
15+
## `header-file` is the file that contains a custom
16+
## header for each of the generated files.
17+
header-file: header.txt
Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
# Regenerating the i18n files
22

3-
The files in this directory are used to generate `stock_strings.dart`, which
4-
is used by the stocks application to look up localized message strings. The
5-
stocks app uses the [Dart `intl` package](https://github.com/dart-lang/intl).
3+
The files in this directory are used to generate `stock_strings.dart`,
4+
which contains the `StockStrings` class. This localizations class is
5+
used by the stocks application to look up localized message strings.
6+
The stocks app uses the [Dart `intl` package](https://github.com/dart-lang/intl).
67

7-
Rebuilding everything requires two steps.
8-
9-
1. Create or update the English and Spanish localizations,
10-
`stocks_en_US.arb`, `stocks_en.arb`, and `stocks_es.arb`. See the
8+
To update the English and Spanish localizations, modify the
9+
`stocks_en_US.arb`, `stocks_en.arb`, or `stocks_es.arb` files. See the
1110
[ARB specification](https://github.com/google/app-resource-bundle/wiki/ApplicationResourceBundleSpecification)
1211
for more info.
1312

14-
2. With `examples/stocks` as the current directory, generate a
15-
`messages_<locale>.dart` for each `stocks_<locale>.arb` file,
16-
`messages_all.dart`, and `stock_strings.dart` with the following command:
17-
18-
```dart
19-
dart ${FLUTTER_PATH}/dev/tools/localization/bin/gen_l10n.dart --arb-dir=lib/i18n \
20-
--template-arb-file=stocks_en.arb --output-localization-file=stock_strings.dart \
21-
--output-class=StockStrings --header-file=header.txt
22-
```
13+
To modify the project's configuration of the localizations tool,
14+
change the `l10n.yaml` file.
2315

2416
The `StockStrings` class creates a delegate that performs message lookups
2517
based on the locale of the device. In this case, the stocks app supports
26-
`en`, `en_US`, and `es`. Thus, the `StockStringsEn` and `StockStringsEs`
27-
classes extends `StockStrings`. `StockStringsEnUs` extends
18+
`en`, `en_US`, and `es` locales. Thus, the `StockStringsEn` and
19+
`StockStringsEs` classes extends `StockStrings`. `StockStringsEnUs` extends
2820
`StockStringsEn`. This allows `StockStringsEnUs` to fall back on messages
29-
in `StockStringsEn`.
21+
in `StockStringsEn`.

dev/benchmarks/test_apps/stocks/lib/i18n/stock_strings.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44

55
import 'dart:async';
66

7+
// ignore: unused_import
78
import 'package:flutter/foundation.dart';
89
import 'package:flutter/widgets.dart';
910
import 'package:flutter_localizations/flutter_localizations.dart';
10-
// ignore: unused_import
1111
import 'package:intl/intl.dart' as intl;
1212

1313
import 'stock_strings_en.dart';
1414
import 'stock_strings_es.dart';
1515

16-
// ignore_for_file: unnecessary_brace_in_string_interps
17-
1816
/// Callers can lookup localized strings with an instance of StockStrings returned
1917
/// by `StockStrings.of(context)`.
2018
///
@@ -42,8 +40,7 @@ import 'stock_strings_es.dart';
4240
/// # Internationalization support.
4341
/// flutter_localizations:
4442
/// sdk: flutter
45-
/// intl: 0.16.0
46-
/// intl_translation: 0.17.7
43+
/// intl: 0.16.1
4744
///
4845
/// # rest of dependencies
4946
/// ```
@@ -99,7 +96,7 @@ abstract class StockStrings {
9996
/// A list of this localizations delegate's supported locales.
10097
static const List<Locale> supportedLocales = <Locale>[
10198
Locale('en'),
102-
Locale('en, US'),
99+
Locale('en', 'US'),
103100
Locale('es')
104101
];
105102

@@ -129,15 +126,24 @@ class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> {
129126
}
130127

131128
StockStrings _lookupStockStrings(Locale locale) {
132-
switch(locale.languageCode) {
129+
130+
131+
// Lookup logic when language+country codes are specified.
132+
switch (locale.languageCode) {
133133
case 'en': {
134134
switch (locale.countryCode) {
135135
case 'US': return StockStringsEnUs();
136136
}
137-
return StockStringsEn();
137+
break;
138138
}
139+
}
140+
141+
// Lookup logic when only language code is specified.
142+
switch (locale.languageCode) {
143+
case 'en': return StockStringsEn();
139144
case 'es': return StockStringsEs();
140145
}
146+
141147
assert(false, 'StockStrings.delegate failed to load unsupported locale "$locale"');
142148
return null;
143149
}

0 commit comments

Comments
 (0)