| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * This provides a basic example of internationalization usage. It uses the | 6 * This provides a basic example of internationalization usage. It uses the |
| 7 * local variant of all the facilities, meaning that libraries with the | 7 * local variant of all the facilities, meaning that libraries with the |
| 8 * data for all the locales are directly imported by the program. More realistic | 8 * data for all the locales are directly imported by the program. Once lazy |
| 9 * examples might read the data from files or over the web, which uses the | 9 * loading is available, we expect this to be the preferred mode, with |
| 10 * same APIs but requires different imports. | 10 * the initialization code actually loading the specific libraries needed. |
| 11 * | 11 * |
| 12 * This defines messages for an English locale directly in the program and | 12 * This defines messages for an English locale directly in the program and |
| 13 * has separate libraries that define German and Thai messages that say more or | 13 * has separate libraries that define German and Thai messages that say more or |
| 14 * less the same thing, and prints the message with the date and time in it | 14 * less the same thing, and prints the message with the date and time in it |
| 15 * formatted appropriately for the locale. | 15 * formatted appropriately for the locale. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 library intl_basic_example; | 18 library intl_basic_example; |
| 19 // These can be replaced with package:intl/... references if using this in | |
| 20 // a separate package. | |
| 21 // TODO(alanknight): Replace these with package: once pub works in buildbots. | |
| 22 import 'dart:async'; | 19 import 'dart:async'; |
| 23 import '../../lib/date_symbol_data_local.dart'; | 20 import 'package:intl/date_symbol_data_local.dart'; |
| 24 import '../../lib/intl.dart'; | 21 import 'package:intl/intl.dart'; |
| 25 import '../../lib/message_lookup_local.dart'; | 22 import 'package:intl/message_lookup_by_library.dart'; |
| 26 import 'messages_all.dart'; | 23 import 'messages_all.dart'; |
| 27 | 24 |
| 25 /** |
| 26 * In order to use this both as an example and as a test case, we pass in |
| 27 * the function for what we're going to do with the output. For a simple |
| 28 * example we just pass in [print] and for tests we pass in a function that |
| 29 * adds it a list to be verified. |
| 30 */ |
| 28 Function doThisWithTheOutput; | 31 Function doThisWithTheOutput; |
| 29 | 32 |
| 30 void setup(Function program, Function output) { | 33 void setup(Function program, Function output) { |
| 31 // Before we use any messages or use date formatting for a locale we must | 34 // Before we use any messages or use date formatting for a locale we must |
| 32 // call their initializtion messages, which are asynchronous, since they | 35 // call their initializtion messages, which are asynchronous, since they |
| 33 // might be reading information from files or over the web. Since we are | 36 // might be reading information from files or over the web. Since we are |
| 34 // running here in local mode they will all complete immediately. | 37 // running here in local mode they will all complete immediately. |
| 35 doThisWithTheOutput = output; | 38 doThisWithTheOutput = output; |
| 36 var germanDatesFuture = initializeDateFormatting('de_DE', null); | 39 var germanDatesFuture = initializeDateFormatting('de_DE', null); |
| 37 var thaiDatesFuture = initializeDateFormatting('th_TH', null); | 40 var thaiDatesFuture = initializeDateFormatting('th_TH', null); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 51 var th = new Intl('th_TH'); | 54 var th = new Intl('th_TH'); |
| 52 // This defines a message that can be internationalized. It is written as a | 55 // This defines a message that can be internationalized. It is written as a |
| 53 // function that returns the result of an Intl.message call. The primary | 56 // function that returns the result of an Intl.message call. The primary |
| 54 // parameter is a string that may use interpolation. | 57 // parameter is a string that may use interpolation. |
| 55 runAt(time, date) => | 58 runAt(time, date) => |
| 56 Intl.message('Ran at $time on $date', name: 'runAt', args: [time, date]); | 59 Intl.message('Ran at $time on $date', name: 'runAt', args: [time, date]); |
| 57 printForLocale(aDate, new Intl(), runAt); | 60 printForLocale(aDate, new Intl(), runAt); |
| 58 printForLocale(aDate, de, runAt); | 61 printForLocale(aDate, de, runAt); |
| 59 printForLocale(aDate, th, runAt); | 62 printForLocale(aDate, th, runAt); |
| 60 // Example making use of the return value from withLocale; | 63 // Example making use of the return value from withLocale; |
| 61 Intl.withLocale(th.locale, () => runAt('now', 'today')) | 64 var returnValue = Intl.withLocale(th.locale, () => runAt('now', 'today')); |
| 62 .then(doThisWithTheOutput); | 65 doThisWithTheOutput(returnValue); |
| 63 } | 66 } |
| 64 | 67 |
| 65 printForLocale(aDate, intl, operation) { | 68 printForLocale(aDate, intl, operation) { |
| 66 var hmsFormat = intl.date().add_Hms(); | 69 var hmsFormat = intl.date().add_Hms(); |
| 67 var dayFormat = intl.date().add_yMMMMEEEEd(); | 70 var dayFormat = intl.date().add_yMMMMEEEEd(); |
| 68 var time = hmsFormat.format(aDate); | 71 var time = hmsFormat.format(aDate); |
| 69 var day = dayFormat.format(aDate); | 72 var day = dayFormat.format(aDate); |
| 70 Intl.withLocale(intl.locale, () { | 73 Intl.withLocale(intl.locale, () => doThisWithTheOutput(operation(time,day))); |
| 71 operation(time,day).then(doThisWithTheOutput); | |
| 72 }); | |
| 73 } | 74 } |
| OLD | NEW |