Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: pkg/intl/example/basic/basic_example.dart

Issue 10915215: Add async initialization for Intl.message and an example (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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. More realistic
9 * examples might read the data from files or over the web, which uses the
10 * same APIs but requires different imports.
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 // These can be replaced with package:intl/... references if using this in
20 // a separate package.
Emily Fortuna 2012/09/12 01:23:48 why not use package:intl references here too to se
Alan Knight 2012/09/12 20:58:20 Sadly, I had them, but took them out because I set
Emily Fortuna 2012/09/12 21:55:59 Ah. Fair enough. Can we add a TODO?
Alan Knight 2012/09/13 18:20:08 Done.
21 #import('../../date_format.dart');
22 #import('../../date_symbol_data_local.dart');
23 #import('../../intl.dart');
24 #import('../../message_lookup_local.dart');
25 #import('messages_all.dart');
26
27 Function doThisWithTheOutput;
28
29 void setup(Function program, Function output) {
30 // Before we use any messages or use date formatting for a locale we must
31 // call their initializtion messages, which are asynchronous, since they
32 // might be reading information from files or over the web. Since we are
33 // running here in local mode they will all complete immediately.
34 doThisWithTheOutput = output;
35 var germanDatesFuture = initializeDateFormatting('de_DE', null);
36 var thaiDatesFuture = initializeDateFormatting('th_TH', null);
37 var germanMessagesFuture = initializeMessages('de_DE');
38 var thaiMessagesFuture = initializeMessages('th_TH');
39 Futures.wait([germanDatesFuture, thaiDatesFuture, germanMessagesFuture,
40 thaiMessagesFuture]).then(program);
41 }
42
43 // Because the initialization messages return futures we split out the main
44 // part of our program into a separate function that runs once all the
45 // futures have completed. We are passed the collection of futures, but we
46 // don't need to use them, so ignore the parameter.
47 runProgram(_) {
Emily Fortuna 2012/09/12 01:23:48 I don't quite understand why you're passed a colle
Alan Knight 2012/09/12 20:58:20 runProgram is called in the "then" call from Futur
Emily Fortuna 2012/09/12 21:55:59 Fair enough. I'm convinced. Feel free to disregard
Alan Knight 2012/09/13 18:20:08 Done :-)
48 var aDate = new Date.fromMillisecondsSinceEpoch(0, true);
49 var de = new Intl('de_DE');
50 var th = new Intl('th_TH');
51 // This defines a message that can be internationalized. It is written as a
52 // function that returns the result of an Intl.message call. The primary
53 // parameter is a string that may use interpolation.
54 runAt(time, date) =>
55 Intl.message('Ran at $time on $date', name: 'runAt', args: [time, date]);
56 printForLocale(aDate, new Intl(), runAt);
57 printForLocale(aDate, de, runAt);
58 printForLocale(aDate, th, runAt);
59 }
60
61 printForLocale(aDate, intl, operation) {
62 var hmsFormat = intl.date().add_Hms();
63 var dayFormat = intl.date().add_yMMMMEEEEd();
64 var time = hmsFormat.format(aDate);
65 var day = dayFormat.format(aDate);
66 Intl.withLocale(intl.locale, () { doThisWithTheOutput(operation(time,day));});
67 }
OLDNEW
« no previous file with comments | « no previous file | pkg/intl/example/basic/basic_example_shell.dart » ('j') | pkg/intl/example/basic/basic_example_shell.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698