Commit e08d446e authored by Jason Simmons's avatar Jason Simmons

Example of using the Dart internationalization package in Flutter

parent a2b8f8b9
/**
* 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<String, Function> _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);
}
/**
* 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
/**
* 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
{
"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": {}
}
}
{
"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": {}
}
}
...@@ -13,8 +13,10 @@ import 'package:flutter/material.dart'; ...@@ -13,8 +13,10 @@ import 'package:flutter/material.dart';
import 'package:flutter/painting.dart'; import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart'; import 'package:flutter/scheduler.dart';
import 'package:intl/intl.dart';
import 'stock_data.dart'; import 'stock_data.dart';
import 'i18n/stock_messages_all.dart';
part 'stock_arrow.dart'; part 'stock_arrow.dart';
part 'stock_home.dart'; part 'stock_home.dart';
...@@ -22,6 +24,7 @@ part 'stock_list.dart'; ...@@ -22,6 +24,7 @@ part 'stock_list.dart';
part 'stock_menu.dart'; part 'stock_menu.dart';
part 'stock_row.dart'; part 'stock_row.dart';
part 'stock_settings.dart'; part 'stock_settings.dart';
part 'stock_strings.dart';
part 'stock_symbol_viewer.dart'; part 'stock_symbol_viewer.dart';
part 'stock_types.dart'; part 'stock_types.dart';
...@@ -105,5 +108,7 @@ class StocksAppState extends State<StocksApp> { ...@@ -105,5 +108,7 @@ class StocksAppState extends State<StocksApp> {
} }
void main() { void main() {
runApp(new StocksApp()); initializeMessages(Intl.defaultLocale).then((_) {
runApp(new StocksApp());
});
} }
...@@ -147,7 +147,7 @@ class StockHomeState extends State<StockHome> { ...@@ -147,7 +147,7 @@ class StockHomeState extends State<StockHome> {
Widget buildToolBar() { Widget buildToolBar() {
return new ToolBar( return new ToolBar(
elevation: 0, elevation: 0,
center: new Text('Stocks'), center: new Text(StockStrings.title()),
right: <Widget>[ right: <Widget>[
new IconButton( new IconButton(
icon: "action/search", icon: "action/search",
...@@ -161,8 +161,8 @@ class StockHomeState extends State<StockHome> { ...@@ -161,8 +161,8 @@ class StockHomeState extends State<StockHome> {
tabBar: new TabBar( tabBar: new TabBar(
selection: _tabBarSelection, selection: _tabBarSelection,
labels: <TabLabel>[ labels: <TabLabel>[
const TabLabel(text: 'MARKET'), new TabLabel(text: StockStrings.market()),
const TabLabel(text: 'PORTFOLIO')] new TabLabel(text: StockStrings.portfolio())]
) )
); );
} }
......
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'
);
}
...@@ -2,3 +2,5 @@ name: stocks ...@@ -2,3 +2,5 @@ name: stocks
dependencies: dependencies:
flutter: flutter:
path: ../../packages/flutter path: ../../packages/flutter
intl: '>=0.12.4+2 <0.13.0'
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