From e08d446ebff5e6e0787656276bd204ffd08d460b Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 3 Dec 2015 15:03:19 -0800 Subject: [PATCH] Example of using the Dart internationalization package in Flutter --- .../stocks/lib/i18n/stock_messages_all.dart | 45 +++++++++++++++++++ .../stocks/lib/i18n/stock_messages_en.dart | 29 ++++++++++++ .../stocks/lib/i18n/stock_messages_es.dart | 29 ++++++++++++ examples/stocks/lib/i18n/stocks_en.arb | 22 +++++++++ examples/stocks/lib/i18n/stocks_es.arb | 22 +++++++++ examples/stocks/lib/main.dart | 7 ++- examples/stocks/lib/stock_home.dart | 6 +-- examples/stocks/lib/stock_strings.dart | 29 ++++++++++++ examples/stocks/pubspec.yaml | 2 + 9 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 examples/stocks/lib/i18n/stock_messages_all.dart create mode 100644 examples/stocks/lib/i18n/stock_messages_en.dart create mode 100644 examples/stocks/lib/i18n/stock_messages_es.dart create mode 100644 examples/stocks/lib/i18n/stocks_en.arb create mode 100644 examples/stocks/lib/i18n/stocks_es.arb create mode 100644 examples/stocks/lib/stock_strings.dart diff --git a/examples/stocks/lib/i18n/stock_messages_all.dart b/examples/stocks/lib/i18n/stock_messages_all.dart new file mode 100644 index 00000000000..eec8fd5675c --- /dev/null +++ b/examples/stocks/lib/i18n/stock_messages_all.dart @@ -0,0 +1,45 @@ +/** + * DO NOT EDIT. This is code generated via package:intl/generate_localized.dart + * This is a library that looks up messages for specific locales by + * delegating to the appropriate library. + */ + +library messages_all; + +import 'dart:async'; +import 'package:intl/message_lookup_by_library.dart'; +import 'package:intl/src/intl_helpers.dart'; +import 'package:intl/intl.dart'; + +import 'stock_messages_en.dart' as messages_en; +import 'stock_messages_es.dart' as messages_es; + + +Map _deferredLibraries = { + 'en' : () => new Future.value(null), + 'es' : () => new Future.value(null), +}; + +MessageLookupByLibrary _findExact(localeName) { + switch (localeName) { + case 'en' : return messages_en.messages; + case 'es' : return messages_es.messages; + default: return null; + } +} + +/** User programs should call this before using [localeName] for messages.*/ +Future initializeMessages(String localeName) { + initializeInternalMessageLookup(() => new CompositeMessageLookup()); + var lib = _deferredLibraries[Intl.canonicalizedLocale(localeName)]; + var load = lib == null ? new Future.value(false) : lib(); + return load.then((_) => + messageLookup.addLocale(localeName, _findGeneratedMessagesFor)); +} + +MessageLookupByLibrary _findGeneratedMessagesFor(locale) { + var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null, + onFailure: (_) => null); + if (actualLocale == null) return null; + return _findExact(actualLocale); +} diff --git a/examples/stocks/lib/i18n/stock_messages_en.dart b/examples/stocks/lib/i18n/stock_messages_en.dart new file mode 100644 index 00000000000..2f72d9b2816 --- /dev/null +++ b/examples/stocks/lib/i18n/stock_messages_en.dart @@ -0,0 +1,29 @@ +/** + * DO NOT EDIT. This is code generated via package:intl/generate_localized.dart + * This is a library that provides messages for a en locale. All the + * messages from the main program should be duplicated here with the same + * function name. + */ + +library messages_en; +import 'package:intl/intl.dart'; +import 'package:intl/message_lookup_by_library.dart'; + +final messages = new MessageLookup(); + +class MessageLookup extends MessageLookupByLibrary { + + get localeName => 'en'; + static market() => "MARKET"; + + static portfolio() => "PORTFOLIO"; + + static title() => "Stocks"; + + + final messages = const { + "market" : market, + "portfolio" : portfolio, + "title" : title + }; +} \ No newline at end of file diff --git a/examples/stocks/lib/i18n/stock_messages_es.dart b/examples/stocks/lib/i18n/stock_messages_es.dart new file mode 100644 index 00000000000..7eeec5509fc --- /dev/null +++ b/examples/stocks/lib/i18n/stock_messages_es.dart @@ -0,0 +1,29 @@ +/** + * DO NOT EDIT. This is code generated via package:intl/generate_localized.dart + * This is a library that provides messages for a es locale. All the + * messages from the main program should be duplicated here with the same + * function name. + */ + +library messages_es; +import 'package:intl/intl.dart'; +import 'package:intl/message_lookup_by_library.dart'; + +final messages = new MessageLookup(); + +class MessageLookup extends MessageLookupByLibrary { + + get localeName => 'es'; + static market() => "MERCADO"; + + static portfolio() => "CARTERA"; + + static title() => "Acciones"; + + + final messages = const { + "market" : market, + "portfolio" : portfolio, + "title" : title + }; +} \ No newline at end of file diff --git a/examples/stocks/lib/i18n/stocks_en.arb b/examples/stocks/lib/i18n/stocks_en.arb new file mode 100644 index 00000000000..f28c71de16e --- /dev/null +++ b/examples/stocks/lib/i18n/stocks_en.arb @@ -0,0 +1,22 @@ +{ + "title": "Stocks", + "@title": { + "description": "Title for the Stocks application", + "type": "text", + "placeholders": {} + }, + + "market": "MARKET", + "@market": { + "description": "Label for the Market tab", + "type": "text", + "placeholders": {} + }, + + "portfolio": "PORTFOLIO", + "@portfolio": { + "description": "Label for the Portfolio tab", + "type": "text", + "placeholders": {} + } +} diff --git a/examples/stocks/lib/i18n/stocks_es.arb b/examples/stocks/lib/i18n/stocks_es.arb new file mode 100644 index 00000000000..89cff4e9bc0 --- /dev/null +++ b/examples/stocks/lib/i18n/stocks_es.arb @@ -0,0 +1,22 @@ +{ + "title": "Acciones", + "@title": { + "description": "Title for the Stocks application", + "type": "text", + "placeholders": {} + }, + + "market": "MERCADO", + "@market": { + "description": "Label for the Market tab", + "type": "text", + "placeholders": {} + }, + + "portfolio": "CARTERA", + "@portfolio": { + "description": "Label for the Portfolio tab", + "type": "text", + "placeholders": {} + } +} diff --git a/examples/stocks/lib/main.dart b/examples/stocks/lib/main.dart index ee16d55d125..24cde032da8 100644 --- a/examples/stocks/lib/main.dart +++ b/examples/stocks/lib/main.dart @@ -13,8 +13,10 @@ import 'package:flutter/material.dart'; import 'package:flutter/painting.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/scheduler.dart'; +import 'package:intl/intl.dart'; import 'stock_data.dart'; +import 'i18n/stock_messages_all.dart'; part 'stock_arrow.dart'; part 'stock_home.dart'; @@ -22,6 +24,7 @@ part 'stock_list.dart'; part 'stock_menu.dart'; part 'stock_row.dart'; part 'stock_settings.dart'; +part 'stock_strings.dart'; part 'stock_symbol_viewer.dart'; part 'stock_types.dart'; @@ -105,5 +108,7 @@ class StocksAppState extends State { } void main() { - runApp(new StocksApp()); + initializeMessages(Intl.defaultLocale).then((_) { + runApp(new StocksApp()); + }); } diff --git a/examples/stocks/lib/stock_home.dart b/examples/stocks/lib/stock_home.dart index ccc5ee28054..25500e05148 100644 --- a/examples/stocks/lib/stock_home.dart +++ b/examples/stocks/lib/stock_home.dart @@ -147,7 +147,7 @@ class StockHomeState extends State { Widget buildToolBar() { return new ToolBar( elevation: 0, - center: new Text('Stocks'), + center: new Text(StockStrings.title()), right: [ new IconButton( icon: "action/search", @@ -161,8 +161,8 @@ class StockHomeState extends State { tabBar: new TabBar( selection: _tabBarSelection, labels: [ - const TabLabel(text: 'MARKET'), - const TabLabel(text: 'PORTFOLIO')] + new TabLabel(text: StockStrings.market()), + new TabLabel(text: StockStrings.portfolio())] ) ); } diff --git a/examples/stocks/lib/stock_strings.dart b/examples/stocks/lib/stock_strings.dart new file mode 100644 index 00000000000..77b95350444 --- /dev/null +++ b/examples/stocks/lib/stock_strings.dart @@ -0,0 +1,29 @@ +part of stocks; + +// 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 + +class StockStrings { + static String title() => Intl.message( + 'Stocks', + name: 'title', + desc: 'Title for the Stocks application' + ); + + static String market() => Intl.message( + 'MARKET', + name: 'market', + desc: 'Label for the Market tab' + ); + + static String portfolio() => Intl.message( + 'PORTFOLIO', + name: 'portfolio', + desc: 'Label for the Portfolio tab' + ); +} diff --git a/examples/stocks/pubspec.yaml b/examples/stocks/pubspec.yaml index a48c3431e82..d633a460083 100644 --- a/examples/stocks/pubspec.yaml +++ b/examples/stocks/pubspec.yaml @@ -2,3 +2,5 @@ name: stocks dependencies: flutter: path: ../../packages/flutter + + intl: '>=0.12.4+2 <0.13.0'