mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Merge pull request #714 from jason-simmons/stocks_i18n
Example of using the Dart internationalization package in Flutter
This commit is contained in:
commit
87667cb7cc
45
examples/stocks/lib/i18n/stock_messages_all.dart
Normal file
45
examples/stocks/lib/i18n/stock_messages_all.dart
Normal file
@ -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<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);
|
||||
}
|
||||
29
examples/stocks/lib/i18n/stock_messages_en.dart
Normal file
29
examples/stocks/lib/i18n/stock_messages_en.dart
Normal file
@ -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
|
||||
};
|
||||
}
|
||||
29
examples/stocks/lib/i18n/stock_messages_es.dart
Normal file
29
examples/stocks/lib/i18n/stock_messages_es.dart
Normal file
@ -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
|
||||
};
|
||||
}
|
||||
22
examples/stocks/lib/i18n/stocks_en.arb
Normal file
22
examples/stocks/lib/i18n/stocks_en.arb
Normal file
@ -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": {}
|
||||
}
|
||||
}
|
||||
22
examples/stocks/lib/i18n/stocks_es.arb
Normal file
22
examples/stocks/lib/i18n/stocks_es.arb
Normal file
@ -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": {}
|
||||
}
|
||||
}
|
||||
@ -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<StocksApp> {
|
||||
}
|
||||
|
||||
void main() {
|
||||
runApp(new StocksApp());
|
||||
initializeMessages(Intl.defaultLocale).then((_) {
|
||||
runApp(new StocksApp());
|
||||
});
|
||||
}
|
||||
|
||||
@ -147,7 +147,7 @@ class StockHomeState extends State<StockHome> {
|
||||
Widget buildToolBar() {
|
||||
return new ToolBar(
|
||||
elevation: 0,
|
||||
center: new Text('Stocks'),
|
||||
center: new Text(StockStrings.title()),
|
||||
right: <Widget>[
|
||||
new IconButton(
|
||||
icon: "action/search",
|
||||
@ -161,8 +161,8 @@ class StockHomeState extends State<StockHome> {
|
||||
tabBar: new TabBar(
|
||||
selection: _tabBarSelection,
|
||||
labels: <TabLabel>[
|
||||
const TabLabel(text: 'MARKET'),
|
||||
const TabLabel(text: 'PORTFOLIO')]
|
||||
new TabLabel(text: StockStrings.market()),
|
||||
new TabLabel(text: StockStrings.portfolio())]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
29
examples/stocks/lib/stock_strings.dart
Normal file
29
examples/stocks/lib/stock_strings.dart
Normal file
@ -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'
|
||||
);
|
||||
}
|
||||
@ -2,3 +2,5 @@ name: stocks
|
||||
dependencies:
|
||||
flutter:
|
||||
path: ../../packages/flutter
|
||||
|
||||
intl: '>=0.12.4+2 <0.13.0'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user