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

Side by Side Diff: pkg/intl/intl.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
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 * Internationalization object providing access to message formatting objects, 6 * Internationalization object providing access to message formatting objects,
7 * date formatting, parsing, bidirectional text relative to a specific locale. 7 * date formatting, parsing, bidirectional text relative to a specific locale.
8 */ 8 */
9 #library('intl'); 9 #library('intl');
10 10
11 #import('../../pkg/htmlescape/htmlescape.dart'); 11 #import('../htmlescape/htmlescape.dart');
12 12
13 #import('date_format.dart'); 13 #import('date_format.dart');
14 #source('intl_message.dart'); 14 #import('lib/intl_helpers.dart');
Emily Fortuna 2012/09/12 01:23:48 I don't see any "intl_helpers.dart" did this not g
Alan Knight 2012/09/12 20:58:20 It's in the /lib directory, but should be there. h
15
15 #source('bidi_formatter.dart'); 16 #source('bidi_formatter.dart');
16 #source('bidi_utils.dart'); 17 #source('bidi_utils.dart');
17 18
18 class Intl { 19 class Intl {
19 /** 20 /**
20 * String indicating the locale code with which the message is to be 21 * String indicating the locale code with which the message is to be
21 * formatted (such as en-CA). 22 * formatted (such as en-CA).
22 */ 23 */
23 static String _locale; 24 String _locale;
24 25
25 IntlMessage intlMsg; 26 /** The default locale, which normally will be obtained from the browser. */
27 static String _defaultLocale;
26 28
27 /** 29 /**
28 * Return a new date format using the specified [pattern]. 30 * Return a new date format using the specified [pattern].
29 * If [desiredLocale] is not specified, then we default to [locale]. 31 * If [desiredLocale] is not specified, then we default to [locale].
30 */ 32 */
31 DateFormat date(String pattern, [String desiredLocale]) { 33 DateFormat date([String pattern, String desiredLocale]) {
32 var actualLocale = (desiredLocale == null) ? _locale : desiredLocale; 34 var actualLocale = (desiredLocale == null) ? locale : desiredLocale;
33 return new DateFormat(pattern, actualLocale); 35 return new DateFormat(pattern, actualLocale);
34 } 36 }
35 37
36 /** 38 /**
37 * Constructor optionally [_locale] for specifics of the language 39 * Constructor optionally [aLocale] for specifics of the language
38 * locale to be used, otherwise, we will attempt to infer it (acceptable if 40 * locale to be used, otherwise, we will attempt to infer it (acceptable if
39 * Dart is running on the client, we can infer from the browser/client 41 * Dart is running on the client, we can infer from the browser/client
40 * preferences). 42 * preferences).
41 */ 43 */
42 Intl([a_locale]) { 44 Intl([String aLocale]) {
43 if (a_locale == null) { 45 if (aLocale != null) {
44 _locale = _getDefaultLocale(); 46 _locale = aLocale;
Emily Fortuna 2012/09/12 01:23:48 why not set a default here? then you don't have to
Alan Knight 2012/09/12 20:58:20 Done.
45 } else {
46 _locale = verifiedLocale(a_locale);
47 } 47 }
48 intlMsg = new IntlMessage(_locale);
49 } 48 }
50 49
51 /** 50 /**
52 * Create a message that can be internationalized. It takes a 51 * Returns a message that can be internationalized. It takes a
53 * [message_str] that will be translated, which may be interpolated 52 * [message_str] that will be translated, which may be interpolated
54 * based on one or more variables, a [desc] providing a description of usage 53 * based on one or more variables, a [desc] providing a description of usage
55 * for the [message_str], and a map of [examples] for each data element to be 54 * for the [message_str], and a map of [examples] for each data element to be
56 * substituted into the message. For example, if message="Hello, $name", then 55 * substituted into the message. For example, if message="Hello, $name", then
57 * examples = {'name': 'Sparky'}. If not using the user's default locale, or 56 * examples = {'name': 'Sparky'}. If not using the user's default locale, or
58 * if the locale is not easily detectable, explicitly pass [locale]. 57 * if the locale is not easily detectable, explicitly pass [locale].
59 * The values of [desc] and [examples] are not used at run-time but are only 58 * The values of [desc] and [examples] are not used at run-time but are only
60 * made available to the translators, so they MUST be simple Strings available 59 * made available to the translators, so they MUST be simple Strings available
61 * at compile time: no String interpolation or concatenation. 60 * at compile time: no String interpolation or concatenation.
62 * The expected usage of this is inside a function that takes as parameters 61 * The expected usage of this is inside a function that takes as parameters
63 * the variables used in the interpolated string, and additionally also a 62 * the variables used in the interpolated string, and additionally also a
64 * locale (optional). 63 * locale (optional).
64 * Ultimately, the information about the enclosing function and its arguments
65 * will be extracted automatically but for the time being it must be passed
66 * explicitly in the [name] and [args] arguments.
65 */ 67 */
66 static String message(String message_str, [final String desc='', 68 static String message(String message_str, [final String desc='',
67 final Map examples=const {}, String locale='']) { 69 final Map examples=const {}, String locale, String name,
Emily Fortuna 2012/09/12 01:23:48 indent at least two more spaces here
Alan Knight 2012/09/12 20:58:20 Done.
68 return message_str; 70 List<String> args]) {
71 return _messageLookup.
Emily Fortuna 2012/09/12 01:23:48 do the line break at the opening of the ( please i
Emily Fortuna 2012/09/12 01:23:48 indent two less spaces here
Alan Knight 2012/09/12 20:58:20 Done.
Alan Knight 2012/09/12 20:58:20 Done.
72 lookupMessage(message_str, desc, examples, locale, name, args);
69 } 73 }
70 74
71 /** 75 /**
72 * Return the locale for this instance. If none was set, the locale will 76 * Return the locale for this instance. If none was set, the locale will
73 * be the default. 77 * be the default.
74 */ 78 */
75 String get locale => _locale; 79 String get locale {
80 if (_locale == null) {
81 _locale = _getDefaultLocale();}
82 return _locale;
83 }
76 84
77 /** 85 /**
78 * Return true if the locale exists, or if it is null. The null case 86 * Return true if the locale exists, or if it is null. The null case
79 * is interpreted to mean that we use the default locale. 87 * is interpreted to mean that we use the default locale.
80 */ 88 */
81 static bool _localeExists(localeName) { 89 static bool _localeExists(localeName) {
82 return DateFormat.localeExists(localeName); 90 return DateFormat.localeExists(localeName);
83 } 91 }
84 92
85 /** 93 /**
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 144
137 /** 145 /**
138 * Format the given function with a specific [locale], given a 146 * Format the given function with a specific [locale], given a
139 * [msg_function] that takes no parameters and returns a String. We 147 * [msg_function] that takes no parameters and returns a String. We
140 * basically delay calling the message function proper until after the proper 148 * basically delay calling the message function proper until after the proper
141 * locale has been set. 149 * locale has been set.
142 */ 150 */
143 static String withLocale(String locale, Function msg_function) { 151 static String withLocale(String locale, Function msg_function) {
144 // We have to do this silliness because Locale is not known at compile time, 152 // We have to do this silliness because Locale is not known at compile time,
145 // but must be a static variable. 153 // but must be a static variable.
146 if (_locale == null) _locale = _getDefaultLocale(); 154 if (_defaultLocale == null) _defaultLocale = _getDefaultLocale();
147 var oldLocale = _locale; 155 var oldLocale = _defaultLocale;
148 _locale = locale; 156 _defaultLocale = locale;
149 var result = msg_function(); 157 var result = msg_function();
150 _locale = oldLocale; 158 _defaultLocale = oldLocale;
151 return result; 159 return result;
152 } 160 }
153 161
154 /** 162 /**
155 * Support method for message formatting. Select the correct exact (gender, 163 * Support method for message formatting. Select the correct exact (gender,
156 * usually) form from [cases] given the user [choice]. 164 * usually) form from [cases] given the user [choice].
157 */ 165 */
158 static String select(String choice, Map cases) { 166 static String select(String choice, Map cases) {
159 if (cases.containsKey(choice)) { 167 if (cases.containsKey(choice)) {
160 return cases[choice]; 168 return cases[choice];
(...skipping 14 matching lines...) Expand all
175 // Yay, hard-coding for now! 183 // Yay, hard-coding for now!
176 return 'en_US'; 184 return 'en_US';
177 } 185 }
178 186
179 /** 187 /**
180 * Accessor for the current locale. This should always == the default locale, 188 * Accessor for the current locale. This should always == the default locale,
181 * unless for some reason this gets called inside a message that resets the 189 * unless for some reason this gets called inside a message that resets the
182 * locale. 190 * locale.
183 */ 191 */
184 static String getCurrentLocale() { 192 static String getCurrentLocale() {
185 return _locale; 193 if (_defaultLocale == null) _defaultLocale = _getDefaultLocale();
194 return _defaultLocale;
186 } 195 }
187 } 196 }
197
198 /** The internal mechanism for looking up messages. We expect this to be set
Emily Fortuna 2012/09/12 01:23:48 /** * The internal.... */
Alan Knight 2012/09/12 20:58:20 Done.
199 * by the implementing package so that we're not dependent on its
200 * implementation.
201 */
202 var _messageLookup = const
203 UninitializedLocaleData('initializeMessages(<locale>)');
204
205 /** Initialize the message lookup mechanism. This is for internal use only.
Emily Fortuna 2012/09/12 01:23:48 same formatting here
Alan Knight 2012/09/12 20:58:20 Done.
206 * User applications should import message_lookup_local.dart and call
207 * initializeMessages
208 */
209 void initializeInternalMessageLookup(Function lookupFunction) {
210 if (_messageLookup is UninitializedLocaleData) {
211 _messageLookup = lookupFunction();
212 }
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698