Unverified Commit 39ffce3e authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

Update stocks example to use l10n.yaml workflow (#58121)

parent 7cdf26d0
# Options used by the localizations tool
## `arb-dir` sets the input directory. The output directory will match
## the input directory if the output directory is not set.
arb-dir: lib/i18n
## `template-arb-file` describes the template arb file that the tool
## will use to check and validate the remaining arb files when
## generating Flutter's localization files.
template-arb-file: stocks_en.arb
## `output-localization-file` is the name of the generated file.
output-localization-file: stock_strings.dart
## `output-class` is the name of the localizations class your
## Flutter application will use. The file will need to be
## imported throughout your application.
output-class: StockStrings
## `header-file` is the file that contains a custom
## header for each of the generated files.
header-file: header.txt
# Regenerating the i18n files # Regenerating the i18n files
The files in this directory are used to generate `stock_strings.dart`, which The files in this directory are used to generate `stock_strings.dart`,
is used by the stocks application to look up localized message strings. The which contains the `StockStrings` class. This localizations class is
stocks app uses the [Dart `intl` package](https://github.com/dart-lang/intl). used by the stocks application to look up localized message strings.
The stocks app uses the [Dart `intl` package](https://github.com/dart-lang/intl).
Rebuilding everything requires two steps. To update the English and Spanish localizations, modify the
`stocks_en_US.arb`, `stocks_en.arb`, or `stocks_es.arb` files. See the
1. Create or update the English and Spanish localizations,
`stocks_en_US.arb`, `stocks_en.arb`, and `stocks_es.arb`. See the
[ARB specification](https://github.com/google/app-resource-bundle/wiki/ApplicationResourceBundleSpecification) [ARB specification](https://github.com/google/app-resource-bundle/wiki/ApplicationResourceBundleSpecification)
for more info. for more info.
2. With `examples/stocks` as the current directory, generate a To modify the project's configuration of the localizations tool,
`messages_<locale>.dart` for each `stocks_<locale>.arb` file, change the `l10n.yaml` file.
`messages_all.dart`, and `stock_strings.dart` with the following command:
```dart
dart ${FLUTTER_PATH}/dev/tools/localization/bin/gen_l10n.dart --arb-dir=lib/i18n \
--template-arb-file=stocks_en.arb --output-localization-file=stock_strings.dart \
--output-class=StockStrings --header-file=header.txt
```
The `StockStrings` class creates a delegate that performs message lookups The `StockStrings` class creates a delegate that performs message lookups
based on the locale of the device. In this case, the stocks app supports based on the locale of the device. In this case, the stocks app supports
`en`, `en_US`, and `es`. Thus, the `StockStringsEn` and `StockStringsEs` `en`, `en_US`, and `es` locales. Thus, the `StockStringsEn` and
classes extends `StockStrings`. `StockStringsEnUs` extends `StockStringsEs` classes extends `StockStrings`. `StockStringsEnUs` extends
`StockStringsEn`. This allows `StockStringsEnUs` to fall back on messages `StockStringsEn`. This allows `StockStringsEnUs` to fall back on messages
in `StockStringsEn`. in `StockStringsEn`.
...@@ -4,17 +4,15 @@ ...@@ -4,17 +4,15 @@
import 'dart:async'; import 'dart:async';
// ignore: unused_import
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
// ignore: unused_import
import 'package:intl/intl.dart' as intl; import 'package:intl/intl.dart' as intl;
import 'stock_strings_en.dart'; import 'stock_strings_en.dart';
import 'stock_strings_es.dart'; import 'stock_strings_es.dart';
// ignore_for_file: unnecessary_brace_in_string_interps
/// Callers can lookup localized strings with an instance of StockStrings returned /// Callers can lookup localized strings with an instance of StockStrings returned
/// by `StockStrings.of(context)`. /// by `StockStrings.of(context)`.
/// ///
...@@ -42,8 +40,7 @@ import 'stock_strings_es.dart'; ...@@ -42,8 +40,7 @@ import 'stock_strings_es.dart';
/// # Internationalization support. /// # Internationalization support.
/// flutter_localizations: /// flutter_localizations:
/// sdk: flutter /// sdk: flutter
/// intl: 0.16.0 /// intl: 0.16.1
/// intl_translation: 0.17.7
/// ///
/// # rest of dependencies /// # rest of dependencies
/// ``` /// ```
...@@ -99,7 +96,7 @@ abstract class StockStrings { ...@@ -99,7 +96,7 @@ abstract class StockStrings {
/// A list of this localizations delegate's supported locales. /// A list of this localizations delegate's supported locales.
static const List<Locale> supportedLocales = <Locale>[ static const List<Locale> supportedLocales = <Locale>[
Locale('en'), Locale('en'),
Locale('en, US'), Locale('en', 'US'),
Locale('es') Locale('es')
]; ];
...@@ -129,15 +126,24 @@ class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> { ...@@ -129,15 +126,24 @@ class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> {
} }
StockStrings _lookupStockStrings(Locale locale) { StockStrings _lookupStockStrings(Locale locale) {
switch(locale.languageCode) {
// Lookup logic when language+country codes are specified.
switch (locale.languageCode) {
case 'en': { case 'en': {
switch (locale.countryCode) { switch (locale.countryCode) {
case 'US': return StockStringsEnUs(); case 'US': return StockStringsEnUs();
} }
return StockStringsEn(); break;
}
} }
// Lookup logic when only language code is specified.
switch (locale.languageCode) {
case 'en': return StockStringsEn();
case 'es': return StockStringsEs(); case 'es': return StockStringsEs();
} }
assert(false, 'StockStrings.delegate failed to load unsupported locale "$locale"'); assert(false, 'StockStrings.delegate failed to load unsupported locale "$locale"');
return null; return null;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment