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('../../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'); |
| 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; |
| 45 } else { | 47 } else { |
| 46 _locale = verifiedLocale(a_locale); | 48 _locale = getCurrentLocale(); |
| 47 } | 49 } |
| 48 intlMsg = new IntlMessage(_locale); | |
| 49 } | 50 } |
| 50 | 51 |
| 51 /** | 52 /** |
| 52 * Create a message that can be internationalized. It takes a | 53 * Returns a message that can be internationalized. It takes a |
| 53 * [message_str] that will be translated, which may be interpolated | 54 * [message_str] that will be translated, which may be interpolated |
| 54 * based on one or more variables, a [desc] providing a description of usage | 55 * 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 | 56 * 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 | 57 * substituted into the message. For example, if message="Hello, $name", then |
| 57 * examples = {'name': 'Sparky'}. If not using the user's default locale, or | 58 * examples = {'name': 'Sparky'}. If not using the user's default locale, or |
| 58 * if the locale is not easily detectable, explicitly pass [locale]. | 59 * 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 | 60 * 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 | 61 * made available to the translators, so they MUST be simple Strings available |
| 61 * at compile time: no String interpolation or concatenation. | 62 * at compile time: no String interpolation or concatenation. |
| 62 * The expected usage of this is inside a function that takes as parameters | 63 * 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 | 64 * the variables used in the interpolated string, and additionally also a |
| 64 * locale (optional). | 65 * locale (optional). |
| 66 * Ultimately, the information about the enclosing function and its arguments | |
| 67 * will be extracted automatically but for the time being it must be passed | |
| 68 * explicitly in the [name] and [args] arguments. | |
| 65 */ | 69 */ |
| 66 static String message(String message_str, [final String desc='', | 70 static String message(String message_str, [final String desc='', |
| 67 final Map examples=const {}, String locale='']) { | 71 final Map examples=const {}, String locale, String name, |
| 68 return message_str; | 72 List<String> args]) { |
| 73 return _messageLookup.lookupMessage( | |
| 74 message_str, desc, examples, locale, name, args); | |
| 69 } | 75 } |
| 70 | 76 |
| 71 /** | 77 /** |
| 72 * Return the locale for this instance. If none was set, the locale will | 78 * Return the locale for this instance. If none was set, the locale will |
| 73 * be the default. | 79 * be the default. |
| 74 */ | 80 */ |
| 75 String get locale => _locale; | 81 String get locale => _locale; |
| 76 | 82 |
| 77 /** | 83 /** |
| 78 * Return true if the locale exists, or if it is null. The null case | 84 * 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. | 85 * is interpreted to mean that we use the default locale. |
| 80 */ | 86 */ |
| 81 static bool _localeExists(localeName) { | 87 static bool _localeExists(localeName) { |
| 82 return DateFormat.localeExists(localeName); | 88 return DateFormat.localeExists(localeName); |
| 83 } | 89 } |
| 84 | 90 |
| 85 /** | 91 /** |
| 86 * Given [newLocale] return a locale that we have data for that is similar | 92 * Given [newLocale] return a locale that we have data for that is similar |
| 87 * to it, if possible. | 93 * to it, if possible. |
| 88 * If [newLocale] is found directly, return it. If it can't be found, look up | 94 * If [newLocale] is found directly, return it. If it can't be found, look up |
| 89 * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-' | 95 * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-' |
| 90 * as a separator and changes it into '_' for lookup, and changes the | 96 * as a separator and changes it into '_' for lookup, and changes the |
| 91 * country to uppercase. | 97 * country to uppercase. |
| 92 * Note that null is interpreted as meaning the default locale, so if | 98 * Note that null is interpreted as meaning the default locale, so if |
| 93 * [newLocale] is null it will be returned. | 99 * [newLocale] is null it will be returned. |
| 94 */ | 100 */ |
| 95 static String verifiedLocale(String newLocale) { | 101 static String verifiedLocale(String newLocale) { |
|
Emily Fortuna
2012/09/12 21:55:59
looks like this method is no longer being used (us
Alan Knight
2012/09/13 18:20:08
It's used by DateFormat, because it uses the set o
| |
| 96 if (newLocale == null) return _getDefaultLocale(); | 102 if (newLocale == null) return _getDefaultLocale(); |
| 97 if (_localeExists(newLocale)) { | 103 if (_localeExists(newLocale)) { |
| 98 return newLocale; | 104 return newLocale; |
| 99 } | 105 } |
| 100 for (var each in [_canonicalized(newLocale), _shortLocale(newLocale)]) { | 106 for (var each in [_canonicalized(newLocale), _shortLocale(newLocale)]) { |
| 101 if (_localeExists(each)) { | 107 if (_localeExists(each)) { |
| 102 return each; | 108 return each; |
| 103 } | 109 } |
| 104 } | 110 } |
| 105 throw new IllegalArgumentException("Invalid locale '$newLocale'"); | 111 throw new IllegalArgumentException("Invalid locale '$newLocale'"); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 136 | 142 |
| 137 /** | 143 /** |
| 138 * Format the given function with a specific [locale], given a | 144 * Format the given function with a specific [locale], given a |
| 139 * [msg_function] that takes no parameters and returns a String. We | 145 * [msg_function] that takes no parameters and returns a String. We |
| 140 * basically delay calling the message function proper until after the proper | 146 * basically delay calling the message function proper until after the proper |
| 141 * locale has been set. | 147 * locale has been set. |
| 142 */ | 148 */ |
| 143 static String withLocale(String locale, Function msg_function) { | 149 static String withLocale(String locale, Function msg_function) { |
| 144 // We have to do this silliness because Locale is not known at compile time, | 150 // We have to do this silliness because Locale is not known at compile time, |
| 145 // but must be a static variable. | 151 // but must be a static variable. |
| 146 if (_locale == null) _locale = _getDefaultLocale(); | 152 if (_defaultLocale == null) _defaultLocale = _getDefaultLocale(); |
| 147 var oldLocale = _locale; | 153 var oldLocale = _defaultLocale; |
| 148 _locale = locale; | 154 _defaultLocale = locale; |
| 149 var result = msg_function(); | 155 var result = msg_function(); |
| 150 _locale = oldLocale; | 156 _defaultLocale = oldLocale; |
| 151 return result; | 157 return result; |
| 152 } | 158 } |
| 153 | 159 |
| 154 /** | 160 /** |
| 155 * Support method for message formatting. Select the correct exact (gender, | 161 * Support method for message formatting. Select the correct exact (gender, |
| 156 * usually) form from [cases] given the user [choice]. | 162 * usually) form from [cases] given the user [choice]. |
| 157 */ | 163 */ |
| 158 static String select(String choice, Map cases) { | 164 static String select(String choice, Map cases) { |
| 159 if (cases.containsKey(choice)) { | 165 if (cases.containsKey(choice)) { |
| 160 return cases[choice]; | 166 return cases[choice]; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 175 // Yay, hard-coding for now! | 181 // Yay, hard-coding for now! |
| 176 return 'en_US'; | 182 return 'en_US'; |
| 177 } | 183 } |
| 178 | 184 |
| 179 /** | 185 /** |
| 180 * Accessor for the current locale. This should always == the default locale, | 186 * 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 | 187 * unless for some reason this gets called inside a message that resets the |
| 182 * locale. | 188 * locale. |
| 183 */ | 189 */ |
| 184 static String getCurrentLocale() { | 190 static String getCurrentLocale() { |
| 185 return _locale; | 191 if (_defaultLocale == null) _defaultLocale = _getDefaultLocale(); |
| 192 return _defaultLocale; | |
| 186 } | 193 } |
| 187 } | 194 } |
| 195 | |
| 196 /** | |
| 197 * The internal mechanism for looking up messages. We expect this to be set | |
| 198 * by the implementing package so that we're not dependent on its | |
| 199 * implementation. | |
| 200 */ | |
| 201 var _messageLookup = const | |
| 202 UninitializedLocaleData('initializeMessages(<locale>)'); | |
| 203 | |
| 204 /** | |
| 205 * Initialize the message lookup mechanism. This is for internal use only. | |
| 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 } | |
| OLD | NEW |