Update stocks example to use l10n.yaml workflow (#58121)
This commit is contained in:
parent
7cdf26d0dc
commit
39ffce3e36
17
dev/benchmarks/test_apps/stocks/l10n.yaml
Normal file
17
dev/benchmarks/test_apps/stocks/l10n.yaml
Normal file
@ -0,0 +1,17 @@
|
||||
# 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
|
@ -1,29 +1,21 @@
|
||||
# Regenerating the i18n files
|
||||
|
||||
The files in this directory are used to generate `stock_strings.dart`, which
|
||||
is 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).
|
||||
The files in this directory are used to generate `stock_strings.dart`,
|
||||
which contains the `StockStrings` class. This localizations class is
|
||||
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.
|
||||
|
||||
1. Create or update the English and Spanish localizations,
|
||||
`stocks_en_US.arb`, `stocks_en.arb`, and `stocks_es.arb`. See the
|
||||
To update the English and Spanish localizations, modify the
|
||||
`stocks_en_US.arb`, `stocks_en.arb`, or `stocks_es.arb` files. See the
|
||||
[ARB specification](https://github.com/google/app-resource-bundle/wiki/ApplicationResourceBundleSpecification)
|
||||
for more info.
|
||||
|
||||
2. With `examples/stocks` as the current directory, generate a
|
||||
`messages_<locale>.dart` for each `stocks_<locale>.arb` 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
|
||||
```
|
||||
To modify the project's configuration of the localizations tool,
|
||||
change the `l10n.yaml` file.
|
||||
|
||||
The `StockStrings` class creates a delegate that performs message lookups
|
||||
based on the locale of the device. In this case, the stocks app supports
|
||||
`en`, `en_US`, and `es`. Thus, the `StockStringsEn` and `StockStringsEs`
|
||||
classes extends `StockStrings`. `StockStringsEnUs` extends
|
||||
`en`, `en_US`, and `es` locales. Thus, the `StockStringsEn` and
|
||||
`StockStringsEs` classes extends `StockStrings`. `StockStringsEnUs` extends
|
||||
`StockStringsEn`. This allows `StockStringsEnUs` to fall back on messages
|
||||
in `StockStringsEn`.
|
||||
in `StockStringsEn`.
|
||||
|
@ -4,17 +4,15 @@
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
// ignore: unused_import
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
// ignore: unused_import
|
||||
import 'package:intl/intl.dart' as intl;
|
||||
|
||||
import 'stock_strings_en.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
|
||||
/// by `StockStrings.of(context)`.
|
||||
///
|
||||
@ -42,8 +40,7 @@ import 'stock_strings_es.dart';
|
||||
/// # Internationalization support.
|
||||
/// flutter_localizations:
|
||||
/// sdk: flutter
|
||||
/// intl: 0.16.0
|
||||
/// intl_translation: 0.17.7
|
||||
/// intl: 0.16.1
|
||||
///
|
||||
/// # rest of dependencies
|
||||
/// ```
|
||||
@ -99,7 +96,7 @@ abstract class StockStrings {
|
||||
/// A list of this localizations delegate's supported locales.
|
||||
static const List<Locale> supportedLocales = <Locale>[
|
||||
Locale('en'),
|
||||
Locale('en, US'),
|
||||
Locale('en', 'US'),
|
||||
Locale('es')
|
||||
];
|
||||
|
||||
@ -129,15 +126,24 @@ class _StockStringsDelegate extends LocalizationsDelegate<StockStrings> {
|
||||
}
|
||||
|
||||
StockStrings _lookupStockStrings(Locale locale) {
|
||||
switch(locale.languageCode) {
|
||||
|
||||
|
||||
// Lookup logic when language+country codes are specified.
|
||||
switch (locale.languageCode) {
|
||||
case 'en': {
|
||||
switch (locale.countryCode) {
|
||||
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();
|
||||
}
|
||||
|
||||
assert(false, 'StockStrings.delegate failed to load unsupported locale "$locale"');
|
||||
return null;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user