OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * This provides a basic example of internationalization usage. It uses 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. Once lazy |
| 9 * loading is available, we expect this to be the preferred mode, with |
| 10 * the initialization code actually loading the specific libraries needed. |
| 11 * |
| 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 |
| 14 * less the same thing, and prints the message with the date and time in it |
| 15 * formatted appropriately for the locale. |
| 16 */ |
| 17 |
| 18 library intl_basic_example; |
| 19 |
| 20 import 'dart:async'; |
| 21 import 'package:intl/date_symbol_data_local.dart'; |
| 22 import 'package:intl/intl.dart'; |
| 23 import 'messages_all.dart'; |
| 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 */ |
| 31 Function doThisWithTheOutput; |
| 32 |
| 33 void setup(Function program, Function output) { |
| 34 // Before we use any messages or use date formatting for a locale we must |
| 35 // call their initializtion messages, which are asynchronous, since they |
| 36 // might be reading information from files or over the web. Since we are |
| 37 // running here in local mode they will all complete immediately. |
| 38 doThisWithTheOutput = output; |
| 39 var germanDatesFuture = initializeDateFormatting('de_DE', null); |
| 40 var thaiDatesFuture = initializeDateFormatting('th_TH', null); |
| 41 var germanMessagesFuture = initializeMessages('de_DE'); |
| 42 var thaiMessagesFuture = initializeMessages('th_TH'); |
| 43 Future |
| 44 .wait([ |
| 45 germanDatesFuture, |
| 46 thaiDatesFuture, |
| 47 germanMessagesFuture, |
| 48 thaiMessagesFuture |
| 49 ]) |
| 50 .then(program); |
| 51 } |
| 52 |
| 53 // Because the initialization messages return futures we split out the main |
| 54 // part of our program into a separate function that runs once all the |
| 55 // futures have completed. We are passed the collection of futures, but we |
| 56 // don't need to use them, so ignore the parameter. |
| 57 runProgram(List<Future> _) { |
| 58 var aDate = new DateTime.fromMillisecondsSinceEpoch(0, isUtc: true); |
| 59 var de = new Intl('de_DE'); |
| 60 var th = new Intl('th_TH'); |
| 61 // This defines a message that can be internationalized. It is written as a |
| 62 // function that returns the result of an Intl.message call. The primary |
| 63 // parameter is a string that may use interpolation. |
| 64 runAt(time, date) => |
| 65 Intl.message('Ran at $time on $date', name: 'runAt', args: [time, date]); |
| 66 printForLocale(aDate, new Intl(), runAt); |
| 67 printForLocale(aDate, de, runAt); |
| 68 printForLocale(aDate, th, runAt); |
| 69 // Example making use of the return value from withLocale; |
| 70 var returnValue = Intl.withLocale(th.locale, () => runAt('now', 'today')); |
| 71 doThisWithTheOutput(returnValue); |
| 72 } |
| 73 |
| 74 printForLocale(aDate, intl, operation) { |
| 75 var hmsFormat = intl.date().add_Hms(); |
| 76 var dayFormat = intl.date().add_yMMMMEEEEd(); |
| 77 var time = hmsFormat.format(aDate); |
| 78 var day = dayFormat.format(aDate); |
| 79 Intl.withLocale(intl.locale, () => doThisWithTheOutput(operation(time, day))); |
| 80 } |
OLD | NEW |