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 * This library provides internationalization and localization. This includes | 6 * This library provides internationalization and localization. This includes |
7 * message formatting and replacement, date and number formatting and parsing, | 7 * message formatting and replacement, date and number formatting and parsing, |
8 * and utilities for working with Bidirectional text. | 8 * and utilities for working with Bidirectional text. |
9 * | 9 * |
10 * This is part of the [intl package] | 10 * This is part of the [intl package] |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 181 |
182 /** | 182 /** |
183 * Return true if the locale exists, or if it is null. The null case | 183 * Return true if the locale exists, or if it is null. The null case |
184 * is interpreted to mean that we use the default locale. | 184 * is interpreted to mean that we use the default locale. |
185 */ | 185 */ |
186 static bool _localeExists(localeName) => DateFormat.localeExists(localeName); | 186 static bool _localeExists(localeName) => DateFormat.localeExists(localeName); |
187 | 187 |
188 /** | 188 /** |
189 * Given [newLocale] return a locale that we have data for that is similar | 189 * Given [newLocale] return a locale that we have data for that is similar |
190 * to it, if possible. | 190 * to it, if possible. |
| 191 * |
191 * If [newLocale] is found directly, return it. If it can't be found, look up | 192 * If [newLocale] is found directly, return it. If it can't be found, look up |
192 * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-' | 193 * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-' |
193 * as a separator and changes it into '_' for lookup, and changes the | 194 * as a separator and changes it into '_' for lookup, and changes the |
194 * country to uppercase. | 195 * country to uppercase. |
| 196 * |
| 197 * There is a special case that if a locale named "fallback" is present |
| 198 * and has been initialized, this will return that name. This can be useful |
| 199 * for messages where you don't want to just use the text from the original |
| 200 * source code, but wish to have a universal fallback translation. |
| 201 * |
195 * Note that null is interpreted as meaning the default locale, so if | 202 * Note that null is interpreted as meaning the default locale, so if |
196 * [newLocale] is null it will be returned. | 203 * [newLocale] is null it will be returned. |
197 */ | 204 */ |
198 static String verifiedLocale(String newLocale, Function localeExists, | 205 static String verifiedLocale(String newLocale, Function localeExists, |
199 {Function onFailure: _throwLocaleError}) { | 206 {Function onFailure: _throwLocaleError}) { |
200 // TODO(alanknight): Previously we kept a single verified locale on the Intl | 207 // TODO(alanknight): Previously we kept a single verified locale on the Intl |
201 // object, but with different verification for different uses, that's more | 208 // object, but with different verification for different uses, that's more |
202 // difficult. As a result, we call this more often. Consider keeping | 209 // difficult. As a result, we call this more often. Consider keeping |
203 // verified locales for each purpose if it turns out to be a performance | 210 // verified locales for each purpose if it turns out to be a performance |
204 // issue. | 211 // issue. |
205 if (newLocale == null) { | 212 if (newLocale == null) { |
206 return verifiedLocale(getCurrentLocale(), localeExists, | 213 return verifiedLocale(getCurrentLocale(), localeExists, |
207 onFailure: onFailure); | 214 onFailure: onFailure); |
208 } | 215 } |
209 if (localeExists(newLocale)) { | 216 if (localeExists(newLocale)) { |
210 return newLocale; | 217 return newLocale; |
211 } | 218 } |
212 for (var each in | 219 for (var each in |
213 [canonicalizedLocale(newLocale), shortLocale(newLocale)]) { | 220 [canonicalizedLocale(newLocale), shortLocale(newLocale), "fallback"]) { |
214 if (localeExists(each)) { | 221 if (localeExists(each)) { |
215 return each; | 222 return each; |
216 } | 223 } |
217 } | 224 } |
218 return onFailure(newLocale); | 225 return onFailure(newLocale); |
219 } | 226 } |
220 | 227 |
221 /** | 228 /** |
222 * The default action if a locale isn't found in verifiedLocale. Throw | 229 * The default action if a locale isn't found in verifiedLocale. Throw |
223 * an exception indicating the locale isn't correct. | 230 * an exception indicating the locale isn't correct. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 * unless for some reason this gets called inside a message that resets the | 393 * unless for some reason this gets called inside a message that resets the |
387 * locale. | 394 * locale. |
388 */ | 395 */ |
389 static String getCurrentLocale() { | 396 static String getCurrentLocale() { |
390 if (defaultLocale == null) defaultLocale = systemLocale; | 397 if (defaultLocale == null) defaultLocale = systemLocale; |
391 return defaultLocale; | 398 return defaultLocale; |
392 } | 399 } |
393 | 400 |
394 toString() => "Intl($locale)"; | 401 toString() => "Intl($locale)"; |
395 } | 402 } |
OLD | NEW |