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
|
# 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 @@
|
|||||||
|
|
||||||
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';
|
|||||||
/// # 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 {
|
|||||||
/// 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> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user