Chromium Code Reviews| 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 * 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('date_format.dart'); | 11 #import('date_format.dart'); |
| 12 #import('src/intl_helpers.dart'); | 12 #import('src/intl_helpers.dart'); |
| 13 | 13 |
| 14 #source('bidi_formatter.dart'); | 14 #source('bidi_formatter.dart'); |
| 15 #source('bidi_utils.dart'); | 15 #source('bidi_utils.dart'); |
| 16 | 16 |
| 17 class Intl { | 17 class Intl { |
| 18 /** | 18 /** |
| 19 * String indicating the locale code with which the message is to be | 19 * String indicating the locale code with which the message is to be |
| 20 * formatted (such as en-CA). | 20 * formatted (such as en-CA). |
| 21 */ | 21 */ |
| 22 String _locale; | 22 String _locale; |
| 23 | 23 |
| 24 /** The default locale. This defaults to being set from systemLocale, but | 24 /** The default locale. This defaults to being set from systemLocale, but |
| 25 * can also be set explicitly, and will then apply to any new instances where | 25 * can also be set explicitly, and will then apply to any new instances where |
| 26 * the locale isn't specified. | 26 * the locale isn't specified. |
| 27 */ | 27 */ |
| 28 static String _defaultLocale; | 28 static String _defaultLocale = systemLocale; |
|
Emily Fortuna
2012/10/02 00:17:19
sorry, why are the changes in this file preferable
Alan Knight
2012/10/02 00:24:29
It sets the systemLocale on creation so it can avo
Emily Fortuna
2012/10/02 00:48:35
Seems good if we can initialize systemLocale and _
Alan Knight
2012/10/02 16:21:42
No, we can't, because getting the real value may i
Emily Fortuna
2012/10/02 17:11:37
Right, that's what I figured. So are we going to m
Alan Knight
2012/10/02 20:22:03
I don't want to make withLocale async. The only op
| |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * The system's locale, as obtained from the window.navigator.language | 31 * The system's locale, as obtained from the window.navigator.language |
| 32 * or other operating system mechanism. Note that due to system limitations | 32 * or other operating system mechanism. Note that due to system limitations |
| 33 * this is not automatically set, and must be set by importing one of | 33 * this is not automatically set, and must be set by importing one of |
| 34 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). | 34 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). |
| 35 */ | 35 */ |
| 36 // TODO(alanknight): Detect this without forcing the jump through hoops. | 36 // TODO(alanknight): Detect this without forcing the jump through hoops. |
| 37 // Issue 5171. | 37 // Issue 5171. |
| 38 static String systemLocale = 'en_US'; | 38 static String systemLocale = 'en_US'; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 | 160 |
| 161 /** | 161 /** |
| 162 * Format the given function with a specific [locale], given a | 162 * Format the given function with a specific [locale], given a |
| 163 * [msg_function] that takes no parameters and returns a String. We | 163 * [msg_function] that takes no parameters and returns a String. We |
| 164 * basically delay calling the message function proper until after the proper | 164 * basically delay calling the message function proper until after the proper |
| 165 * locale has been set. | 165 * locale has been set. |
| 166 */ | 166 */ |
| 167 static String withLocale(String locale, Function msg_function) { | 167 static String withLocale(String locale, Function msg_function) { |
| 168 // We have to do this silliness because Locale is not known at compile time, | 168 // We have to do this silliness because Locale is not known at compile time, |
| 169 // but must be a static variable. | 169 // but must be a static variable. |
| 170 if (_defaultLocale == null) _defaultLocale = systemLocale; | 170 var oldLocale = _defaultLocale; |
|
Emily Fortuna
2012/10/02 00:17:19
alert alert spurious whitespace
Alan Knight
2012/10/02 00:24:29
Done.
| |
| 171 var oldLocale = _defaultLocale; | |
| 172 _defaultLocale = locale; | 171 _defaultLocale = locale; |
| 173 var result = msg_function(); | 172 var result = msg_function(); |
| 174 _defaultLocale = oldLocale; | 173 _defaultLocale = oldLocale; |
| 175 return result; | 174 return result; |
| 176 } | 175 } |
| 177 | 176 |
| 178 /** | 177 /** |
| 179 * Support method for message formatting. Select the correct exact (gender, | 178 * Support method for message formatting. Select the correct exact (gender, |
| 180 * usually) form from [cases] given the user [choice]. | 179 * usually) form from [cases] given the user [choice]. |
| 181 */ | 180 */ |
| 182 static String select(String choice, Map cases) { | 181 static String select(String choice, Map cases) { |
| 183 if (cases.containsKey(choice)) { | 182 if (cases.containsKey(choice)) { |
| 184 return cases[choice]; | 183 return cases[choice]; |
| 185 } else if (cases.containsKey('other')){ | 184 } else if (cases.containsKey('other')){ |
| 186 return cases['other']; | 185 return cases['other']; |
| 187 } else { | 186 } else { |
| 188 return ''; | 187 return ''; |
| 189 } | 188 } |
| 190 } | 189 } |
| 191 | 190 |
| 192 /** | 191 /** |
| 193 * Accessor for the current locale. This should always == the default locale, | 192 * Accessor for the current locale. This should always == the default locale, |
| 194 * unless for some reason this gets called inside a message that resets the | 193 * unless for some reason this gets called inside a message that resets the |
| 195 * locale. | 194 * locale. |
| 196 */ | 195 */ |
| 197 static String getCurrentLocale() { | 196 static String getCurrentLocale() { |
| 198 if (_defaultLocale == null) _defaultLocale = systemLocale; | |
| 199 return _defaultLocale; | 197 return _defaultLocale; |
| 200 } | 198 } |
| 201 } | 199 } |
| 202 | 200 |
| 203 /** | 201 /** |
| 204 * The internal mechanism for looking up messages. We expect this to be set | 202 * The internal mechanism for looking up messages. We expect this to be set |
| 205 * by the implementing package so that we're not dependent on its | 203 * by the implementing package so that we're not dependent on its |
| 206 * implementation. | 204 * implementation. |
| 207 */ | 205 */ |
| 208 var _messageLookup = const | 206 var _messageLookup = const |
| 209 UninitializedLocaleData('initializeMessages(<locale>)'); | 207 UninitializedLocaleData('initializeMessages(<locale>)'); |
| 210 | 208 |
| 211 /** | 209 /** |
| 212 * Initialize the message lookup mechanism. This is for internal use only. | 210 * Initialize the message lookup mechanism. This is for internal use only. |
| 213 * User applications should import message_lookup_local.dart and call | 211 * User applications should import message_lookup_local.dart and call |
| 214 * initializeMessages | 212 * initializeMessages |
| 215 */ | 213 */ |
| 216 void initializeInternalMessageLookup(Function lookupFunction) { | 214 void initializeInternalMessageLookup(Function lookupFunction) { |
| 217 if (_messageLookup is UninitializedLocaleData) { | 215 if (_messageLookup is UninitializedLocaleData) { |
| 218 _messageLookup = lookupFunction(); | 216 _messageLookup = lookupFunction(); |
| 219 } | 217 } |
| 220 } | 218 } |
| OLD | NEW |