| 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 * ## Installing ## | 10 * ## Installing ## |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 * 'other': '$num_people other people'})} in $place.''', | 74 * 'other': '$num_people other people'})} in $place.''', |
| 75 * name: 'msg', | 75 * name: 'msg', |
| 76 * args: [num_people, place], | 76 * args: [num_people, place], |
| 77 * desc: 'Description of how many people are seen as program start.', | 77 * desc: 'Description of how many people are seen as program start.', |
| 78 * examples: {'num_people': 3, 'place': 'London'}); | 78 * examples: {'num_people': 3, 'place': 'London'}); |
| 79 * | 79 * |
| 80 * Calling `msg(2, 'Athens');` would | 80 * Calling `msg(2, 'Athens');` would |
| 81 * produce "I see 2 other people in Athens." as output in the default locale. | 81 * produce "I see 2 other people in Athens." as output in the default locale. |
| 82 * | 82 * |
| 83 * To use a locale other than the default, use the `withLocale` function. | 83 * To use a locale other than the default, use the `withLocale` function. |
| 84 * You can set the default locale. |
| 85 * Intl.defaultLocale = "pt_BR"; |
| 86 * |
| 87 * To temporarily use a locale other than the default, use the `withLocale` |
| 88 * function. |
| 84 * var todayString = new DateFormat("pt_BR").format(new DateTime.now()); | 89 * var todayString = new DateFormat("pt_BR").format(new DateTime.now()); |
| 85 * print(withLocale("pt_BR", () => today(todayString)); | 90 * print(withLocale("pt_BR", () => today(todayString)); |
| 86 * | 91 * |
| 87 * See `tests/message_format_test.dart` for more examples. | 92 * See `tests/message_format_test.dart` for more examples. |
| 88 */ | 93 */ |
| 89 //TODO(efortuna): documentation example involving the offset parameter? | 94 //TODO(efortuna): documentation example involving the offset parameter? |
| 90 | 95 |
| 91 class Intl { | 96 class Intl { |
| 92 /** | 97 /** |
| 93 * String indicating the locale code with which the message is to be | 98 * String indicating the locale code with which the message is to be |
| 94 * formatted (such as en-CA). | 99 * formatted (such as en-CA). |
| 95 */ | 100 */ |
| 96 String _locale; | 101 String _locale; |
| 97 | 102 |
| 98 /** The default locale. This defaults to being set from systemLocale, but | 103 /** The default locale. This defaults to being set from systemLocale, but |
| 99 * can also be set explicitly, and will then apply to any new instances where | 104 * can also be set explicitly, and will then apply to any new instances where |
| 100 * the locale isn't specified. | 105 * the locale isn't specified. |
| 101 */ | 106 */ |
| 102 static String _defaultLocale; | 107 static String defaultLocale; |
| 103 | 108 |
| 104 /** | 109 /** |
| 105 * The system's locale, as obtained from the window.navigator.language | 110 * The system's locale, as obtained from the window.navigator.language |
| 106 * or other operating system mechanism. Note that due to system limitations | 111 * or other operating system mechanism. Note that due to system limitations |
| 107 * this is not automatically set, and must be set by importing one of | 112 * this is not automatically set, and must be set by importing one of |
| 108 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). | 113 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). |
| 109 */ | 114 */ |
| 110 static String systemLocale = 'en_US'; | 115 static String systemLocale = 'en_US'; |
| 111 | 116 |
| 112 /** | 117 /** |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 * | 343 * |
| 339 * In either case, the purpose of this is to delay calling [message_function] | 344 * In either case, the purpose of this is to delay calling [message_function] |
| 340 * until the proper locale has been set. This returns the result of calling | 345 * until the proper locale has been set. This returns the result of calling |
| 341 * [message_function], which could be of an arbitrary type. | 346 * [message_function], which could be of an arbitrary type. |
| 342 */ | 347 */ |
| 343 static withLocale(String locale, Function message_function) { | 348 static withLocale(String locale, Function message_function) { |
| 344 // We have to do this silliness because Locale is not known at compile time, | 349 // We have to do this silliness because Locale is not known at compile time, |
| 345 // but must be a static variable in order to be visible to the Intl.message | 350 // but must be a static variable in order to be visible to the Intl.message |
| 346 // invocation. | 351 // invocation. |
| 347 var oldLocale = getCurrentLocale(); | 352 var oldLocale = getCurrentLocale(); |
| 348 _defaultLocale = locale; | 353 defaultLocale = locale; |
| 349 var result = message_function(); | 354 var result = message_function(); |
| 350 _defaultLocale = oldLocale; | 355 defaultLocale = oldLocale; |
| 351 return result; | 356 return result; |
| 352 } | 357 } |
| 353 | 358 |
| 354 /** | 359 /** |
| 355 * Accessor for the current locale. This should always == the default locale, | 360 * Accessor for the current locale. This should always == the default locale, |
| 356 * unless for some reason this gets called inside a message that resets the | 361 * unless for some reason this gets called inside a message that resets the |
| 357 * locale. | 362 * locale. |
| 358 */ | 363 */ |
| 359 static String getCurrentLocale() { | 364 static String getCurrentLocale() { |
| 360 if (_defaultLocale == null) _defaultLocale = systemLocale; | 365 if (defaultLocale == null) defaultLocale = systemLocale; |
| 361 return _defaultLocale; | 366 return defaultLocale; |
| 362 } | 367 } |
| 363 | 368 |
| 364 toString() => "Intl($locale)"; | 369 toString() => "Intl($locale)"; |
| 365 } | 370 } |
| OLD | NEW |