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 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 * Note that null is interpreted as meaning the default locale, so if | 99 * Note that null is interpreted as meaning the default locale, so if |
| 100 * [newLocale] is null it will be returned. | 100 * [newLocale] is null it will be returned. |
| 101 */ | 101 */ |
| 102 static String verifiedLocale(String newLocale) { | 102 static String verifiedLocale(String newLocale) { |
| 103 // TODO(alanknight): This is specific to DateFormat, and only used there | 103 // TODO(alanknight): This is specific to DateFormat, and only used there |
| 104 // now. This should be moved, renamed, or generalized. | 104 // now. This should be moved, renamed, or generalized. |
| 105 if (newLocale == null) return _getDefaultLocale(); | 105 if (newLocale == null) return _getDefaultLocale(); |
| 106 if (_localeExists(newLocale)) { | 106 if (_localeExists(newLocale)) { |
| 107 return newLocale; | 107 return newLocale; |
| 108 } | 108 } |
| 109 for (var each in [_canonicalized(newLocale), _shortLocale(newLocale)]) { | 109 for (var each in [canonicalizedLocale(newLocale), _shortLocale(newLocale)]) { |
| 110 if (_localeExists(each)) { | 110 if (_localeExists(each)) { |
| 111 return each; | 111 return each; |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 throw new IllegalArgumentException("Invalid locale '$newLocale'"); | 114 throw new IllegalArgumentException("Invalid locale '$newLocale'"); |
| 115 } | 115 } |
| 116 | 116 |
| 117 /** Return the short version of a locale name, e.g. 'en_US' => 'en' */ | 117 /** Return the short version of a locale name, e.g. 'en_US' => 'en' */ |
| 118 static String _shortLocale(String aLocale) { | 118 static String _shortLocale(String aLocale) { |
| 119 if (aLocale.length < 2) return aLocale; | 119 if (aLocale.length < 2) return aLocale; |
| 120 return aLocale.substring(0, 2).toLowerCase(); | 120 return aLocale.substring(0, 2).toLowerCase(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 /** | 123 /** |
| 124 * Return a locale name turned into xx_YY where it might possibly be | 124 * Return a locale name turned into xx_YY where it might possibly be |
| 125 * in the wrong case or with a hyphen instead of an underscore. | 125 * in the wrong case or with a hyphen instead of an underscore. |
| 126 */ | 126 */ |
| 127 static String _canonicalized(String aLocale) { | 127 static String canonicalizedLocale(String aLocale) { |
| 128 // Locales of length < 5 are presumably two-letter forms, or else malformed. | 128 // Locales of length < 5 are presumably two-letter forms, or else malformed. |
| 129 // Locales of length > 6 are likely to be malformed. In either case we | 129 // Locales of length > 6 are likely to be malformed. In either case we |
| 130 // return them unmodified and if correct they will be found. | 130 // return them unmodified and if correct they will be found. |
| 131 if ((aLocale.length < 5) || (aLocale.length > 6)) return aLocale; | 131 if ((aLocale.length < 5) || (aLocale.length > 6)) return aLocale; |
| 132 if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale; | 132 if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale; |
| 133 return '${aLocale[0]}${aLocale[1]}_${aLocale[3].toUpperCase()}' | 133 return '${aLocale[0]}${aLocale[1]}_${aLocale[3].toUpperCase()}' |
| 134 '${aLocale[4].toUpperCase()}'; | 134 '${aLocale[4].toUpperCase()}'; |
| 135 } | 135 } |
| 136 | 136 |
| 137 /** | 137 /** |
| 138 * Support method for message formatting. Select the correct plural form from | 138 * Support method for message formatting. Select the correct plural form from |
| 139 * [cases] given [howMany]. | 139 * [cases] given [howMany]. |
| 140 */ | 140 */ |
| 141 static String plural(var howMany, Map cases, [num offset=0]) { | 141 static String plural(var howMany, Map cases, [num offset=0]) { |
| 142 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! | 142 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! |
| 143 return select(howMany.toString(), cases); | 143 return select(howMany.toString(), cases); |
| 144 } | 144 } |
| 145 | 145 |
| 146 /** | 146 /** |
| 147 * Format the given function with a specific [locale], given a | 147 * Format the given function with a specific [locale], given a |
| 148 * [msg_function] that takes no parameters and returns a String. We | 148 * [msg_function] that takes no parameters and returns a String. We |
| 149 * basically delay calling the message function proper until after the proper | 149 * basically delay calling the message function proper until after the proper |
| 150 * locale has been set. | 150 * locale has been set. |
| 151 */ | 151 */ |
| 152 static String withLocale(String locale, Function msg_function) { | 152 static String withLocale(String locale, Function msg_function) { |
| 153 // We have to do this silliness because Locale is not known at compile time, | 153 // We have to do this silliness because Locale is not known at compile time, |
| 154 // but must be a static variable. | 154 // but must be a static variable. |
| 155 if (_defaultLocale == null) _defaultLocale = _getDefaultLocale(); | 155 if (_defaultLocale == null) _defaultLocale = systemLocale; |
| 156 var oldLocale = _defaultLocale; | 156 var oldLocale = _defaultLocale; |
| 157 _defaultLocale = locale; | 157 _defaultLocale = locale; |
| 158 var result = msg_function(); | 158 var result = msg_function(); |
| 159 _defaultLocale = oldLocale; | 159 _defaultLocale = oldLocale; |
| 160 return result; | 160 return result; |
| 161 } | 161 } |
| 162 | 162 |
| 163 /** | 163 /** |
| 164 * Support method for message formatting. Select the correct exact (gender, | 164 * Support method for message formatting. Select the correct exact (gender, |
| 165 * usually) form from [cases] given the user [choice]. | 165 * usually) form from [cases] given the user [choice]. |
| 166 */ | 166 */ |
| 167 static String select(String choice, Map cases) { | 167 static String select(String choice, Map cases) { |
| 168 if (cases.containsKey(choice)) { | 168 if (cases.containsKey(choice)) { |
| 169 return cases[choice]; | 169 return cases[choice]; |
| 170 } else if (cases.containsKey('other')){ | 170 } else if (cases.containsKey('other')){ |
| 171 return cases['other']; | 171 return cases['other']; |
| 172 } else { | 172 } else { |
| 173 return ''; | 173 return ''; |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 | 176 |
| 177 /** | 177 /** |
| 178 * The system's locale, as obtained from the window.navigator.language | |
| 179 * or other operating system mechanism. Note that due to system limitations | |
| 180 * this is not automatically set, and must be set by importing one of | |
| 181 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). | |
| 182 */ | |
| 183 // TODO(alanknight): Detect this without forcing the jump through hoops. | |
| 184 // Issue 5171. | |
| 185 static String systemLocale = 'en_US'; | |
|
Emily Fortuna
2012/09/17 21:26:38
in that case, should by default we make systemLoca
Alan Knight
2012/09/17 22:16:18
Default: The user may not call either of these mec
| |
| 186 | |
| 187 /** | |
| 178 * Helper to detect the locale as defined at runtime. | 188 * Helper to detect the locale as defined at runtime. |
| 179 */ | 189 */ |
| 180 static String _getDefaultLocale() { | 190 static String _getDefaultLocale() { |
| 181 // TODO(efortuna): Detect the default locale given the user preferences. | 191 return systemLocale; |
| 182 // That would mean using window.navigator.language in a browser or | |
| 183 // an environment variable or other OS mechanism for the standalone VM. | |
| 184 // Yay, hard-coding for now! | |
| 185 return 'en_US'; | |
| 186 } | 192 } |
| 187 | 193 |
| 188 /** | 194 /** |
| 189 * Accessor for the current locale. This should always == the default locale, | 195 * Accessor for the current locale. This should always == the default locale, |
| 190 * unless for some reason this gets called inside a message that resets the | 196 * unless for some reason this gets called inside a message that resets the |
| 191 * locale. | 197 * locale. |
| 192 */ | 198 */ |
| 193 static String getCurrentLocale() { | 199 static String getCurrentLocale() { |
| 194 if (_defaultLocale == null) _defaultLocale = _getDefaultLocale(); | 200 if (_defaultLocale == null) _defaultLocale = systemLocale; |
|
Emily Fortuna
2012/09/17 21:26:38
if you add this systemLocale variable, the necessi
Alan Knight
2012/09/17 22:16:18
Yes. I think I'd thought that but then forgot. Don
| |
| 195 return _defaultLocale; | 201 return _defaultLocale; |
| 196 } | 202 } |
| 197 } | 203 } |
| 198 | 204 |
| 199 /** | 205 /** |
| 200 * The internal mechanism for looking up messages. We expect this to be set | 206 * The internal mechanism for looking up messages. We expect this to be set |
| 201 * by the implementing package so that we're not dependent on its | 207 * by the implementing package so that we're not dependent on its |
| 202 * implementation. | 208 * implementation. |
| 203 */ | 209 */ |
| 204 var _messageLookup = const | 210 var _messageLookup = const |
| 205 UninitializedLocaleData('initializeMessages(<locale>)'); | 211 UninitializedLocaleData('initializeMessages(<locale>)'); |
| 206 | 212 |
| 207 /** | 213 /** |
| 208 * Initialize the message lookup mechanism. This is for internal use only. | 214 * Initialize the message lookup mechanism. This is for internal use only. |
| 209 * User applications should import message_lookup_local.dart and call | 215 * User applications should import message_lookup_local.dart and call |
| 210 * initializeMessages | 216 * initializeMessages |
| 211 */ | 217 */ |
| 212 void initializeInternalMessageLookup(Function lookupFunction) { | 218 void initializeInternalMessageLookup(Function lookupFunction) { |
| 213 if (_messageLookup is UninitializedLocaleData) { | 219 if (_messageLookup is UninitializedLocaleData) { |
| 214 _messageLookup = lookupFunction(); | 220 _messageLookup = lookupFunction(); |
| 215 } | 221 } |
| 216 } | 222 } |
| OLD | NEW |