Updated I18N API for Flutter (#11497)

This commit is contained in:
Hans Muller
2017-08-23 17:07:59 -07:00
committed by GitHub
parent 7676e5119e
commit 8c2c502257
19 changed files with 1065 additions and 241 deletions

View File

@@ -1,10 +1,33 @@
To rebuild the i18n files:
## Regenerating the i18n files
The files in this directory are based on ../lib/stock_strings.dart
which defines all of the localizable strings used by the stocks
app. The stocks app uses
the [Dart `intl` package](https://github.com/dart-lang/intl).
Rebuilding everything requires two steps.
With the `examples/stocks` as the current directory, generate
`intl_messages.arb` from `lib/stock_strings.dart`:
```
pub run intl_translation:generate_from_arb \
--output-dir=lib/i18n \
--generated-file-prefix=stock_ \
--no-use-deferred-loading \
lib/*.dart \
lib/i18n/*.arb
flutter pub pub run intl_translation:extract_to_arb --output-dir=lib/i18n lib/stock_strings.dart
```
The `intl_messages.arb` file is a JSON format map with one entry for
each `Intl.message()` function defined in `stock_strings.dart`. This
file was used to create the English and Spanish localizations,
`stocks_en.arb` and `stocks_es.arb`. The `intl_messages.arb` wasn't
checked into the repository, since it only serves as a template for
the other `.arb` files.
With the `examples/stocks` as the current directory, generate a
`stock_messages_<locale>.dart` for each `stocks_<locale>.arb` file and
`stock_messages_all.dart`, which imports all of the messages files:
```
flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/i18n \
--generated-file-prefix=stock_ --no-use-deferred-loading lib/*.dart lib/i18n/stocks_*.arb
```
The `StockStrings` class uses the generated `initializeMessages()`
function (`stock_messages_all.dart`) to load the localized messages
and `Intl.message()` to look them up.

View File

@@ -11,7 +11,8 @@ import 'package:intl/src/intl_helpers.dart';
import 'stock_messages_en.dart' as messages_en;
import 'stock_messages_es.dart' as messages_es;
Map<String, Function> _deferredLibraries = {
typedef Future<dynamic> LibraryLoader();
Map<String, LibraryLoader> _deferredLibraries = {
'en': () => new Future.value(null),
'es': () => new Future.value(null),
};

View File

@@ -13,9 +13,7 @@ import 'package:flutter/rendering.dart' show
debugPaintLayerBordersEnabled,
debugPaintPointersEnabled,
debugRepaintRainbowEnabled;
import 'package:intl/intl.dart';
import 'i18n/stock_messages_all.dart';
import 'stock_data.dart';
import 'stock_home.dart';
import 'stock_settings.dart';
@@ -23,6 +21,14 @@ import 'stock_strings.dart';
import 'stock_symbol_viewer.dart';
import 'stock_types.dart';
class _StocksLocalizationsDelegate extends LocalizationsDelegate<StockStrings> {
@override
Future<StockStrings> load(Locale locale) => StockStrings.load(locale);
@override
bool shouldReload(_StocksLocalizationsDelegate old) => false;
}
class StocksApp extends StatefulWidget {
@override
StocksAppState createState() => new StocksAppState();
@@ -99,13 +105,6 @@ class StocksAppState extends State<StocksApp> {
return null;
}
Future<LocaleQueryData> _onLocaleChanged(Locale locale) async {
final String localeString = locale.toString();
await initializeMessages(localeString);
Intl.defaultLocale = localeString;
return StockStrings.instance;
}
@override
Widget build(BuildContext context) {
assert(() {
@@ -119,6 +118,9 @@ class StocksAppState extends State<StocksApp> {
return new MaterialApp(
title: 'Stocks',
theme: theme,
localizationsDelegates: <_StocksLocalizationsDelegate>[
new _StocksLocalizationsDelegate(),
],
debugShowMaterialGrid: _configuration.debugShowGrid,
showPerformanceOverlay: _configuration.showPerformanceOverlay,
showSemanticsDebugger: _configuration.showSemanticsDebugger,
@@ -127,7 +129,6 @@ class StocksAppState extends State<StocksApp> {
'/settings': (BuildContext context) => new StockSettings(_configuration, configurationUpdater)
},
onGenerateRoute: _getRoute,
onLocaleChanged: _onLocaleChanged
);
}
}

View File

@@ -2,39 +2,52 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:intl/intl.dart';
import 'package:flutter/widgets.dart';
// Wrappers for strings that are shown in the UI. The strings can be
// translated for different locales using the Dart intl package.
//
// Locale-specific values for the strings live in the i18n/*.arb files.
//
// To generate the stock_messages_*.dart files from the ARB files, run:
// pub run intl:generate_from_arb --output-dir=lib/i18n --generated-file-prefix=stock_ --no-use-deferred-loading lib/stock_strings.dart lib/i18n/stocks_*.arb
import 'i18n/stock_messages_all.dart';
class StockStrings extends LocaleQueryData {
static StockStrings of(BuildContext context) {
return LocaleQuery.of(context);
// Information about how this file relates to i18n/stock_messages_all.dart and how the i18n files
// were generated can be found in i18n/regenerate.md.
class StockStrings {
StockStrings(Locale locale) : _localeName = locale.toString();
final String _localeName;
static Future<StockStrings> load(Locale locale) {
return initializeMessages(locale.toString())
.then((Null _) {
return new StockStrings(locale);
});
}
static final StockStrings instance = new StockStrings();
static StockStrings of(BuildContext context) {
return Localizations.of<StockStrings>(context, StockStrings);
}
String title() => Intl.message(
'Stocks',
name: 'title',
desc: 'Title for the Stocks application'
);
String title() {
return Intl.message(
'<Stocks>',
name: 'title',
desc: 'Title for the Stocks application',
locale: _localeName,
);
}
String market() => Intl.message(
'MARKET',
name: 'market',
desc: 'Label for the Market tab'
desc: 'Label for the Market tab',
locale: _localeName,
);
String portfolio() => Intl.message(
'PORTFOLIO',
name: 'portfolio',
desc: 'Label for the Portfolio tab'
desc: 'Label for the Portfolio tab',
locale: _localeName,
);
}

View File

@@ -13,9 +13,19 @@ void main() {
stocks.main();
await tester.idle(); // see https://github.com/flutter/flutter/issues/1865
await tester.pump();
// The initial test app's locale is "_", so we're seeing the fallback translation here.
expect(find.text('MARKET'), findsOneWidget);
await tester.binding.setLocale('es', 'US');
await tester.idle();
// The Localizations widget has been built with the new locale. The
// new locale's strings are loaded asynchronously, so we're still
// displaying the previous locale's strings.
await tester.pump();
expect(find.text('MARKET'), findsOneWidget);
// The localized strings have finished loading and dependent
// widgets have been updated.
await tester.pump();
expect(find.text('MERCADO'), findsOneWidget);
});