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 * 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] |
| 11 * (https://pub.dartlang.org/packages/intl). | 11 * (https://pub.dartlang.org/packages/intl). |
| 12 * | 12 * |
| 13 * For things that require locale or other data, there are multiple different | 13 * For things that require locale or other data, there are multiple different |
| 14 * ways of making that data available, which may require importing different | 14 * ways of making that data available, which may require importing different |
| 15 * libraries. See the class comments for more details. | 15 * libraries. See the class comments for more details. |
| 16 * | 16 * |
| 17 * There is also a simple example application that can be found in the | 17 * There is also a simple example application that can be found in the |
| 18 * [example/basic](https://github.com/dart-lang/intl/tree/master/example/basic) | 18 * [example/basic](https://github.com/dart-lang/intl/tree/master/example/basic) |
| 19 * directory. | 19 * directory. |
| 20 */ | 20 */ |
| 21 library intl; | 21 library intl; |
| 22 | 22 |
| 23 import 'dart:async'; | |
| 23 import 'dart:collection'; | 24 import 'dart:collection'; |
| 24 import 'dart:convert'; | 25 import 'dart:convert'; |
| 25 import 'dart:math'; | 26 import 'dart:math'; |
| 26 | 27 |
| 27 import 'date_symbols.dart'; | 28 import 'date_symbols.dart'; |
| 28 import 'number_symbols.dart'; | 29 import 'number_symbols.dart'; |
| 29 import 'number_symbols_data.dart'; | 30 import 'number_symbols_data.dart'; |
| 30 import 'src/date_format_internal.dart'; | 31 import 'src/date_format_internal.dart'; |
| 31 import 'src/intl_helpers.dart'; | 32 import 'src/intl_helpers.dart'; |
| 32 | 33 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 */ | 82 */ |
| 82 //TODO(efortuna): documentation example involving the offset parameter? | 83 //TODO(efortuna): documentation example involving the offset parameter? |
| 83 | 84 |
| 84 class Intl { | 85 class Intl { |
| 85 /** | 86 /** |
| 86 * String indicating the locale code with which the message is to be | 87 * String indicating the locale code with which the message is to be |
| 87 * formatted (such as en-CA). | 88 * formatted (such as en-CA). |
| 88 */ | 89 */ |
| 89 String _locale; | 90 String _locale; |
| 90 | 91 |
| 91 /** The default locale. This defaults to being set from systemLocale, but | 92 /** |
| 93 * The default locale. This defaults to being set from systemLocale, but | |
| 92 * can also be set explicitly, and will then apply to any new instances where | 94 * can also be set explicitly, and will then apply to any new instances where |
| 93 * the locale isn't specified. | 95 * the locale isn't specified. |
| 94 */ | 96 */ |
| 95 static String defaultLocale; | 97 static String get defaultLocale { |
| 98 var zoneLocale = Zone.current[#Intl_locale]; | |
|
Siggi Cherem (dart-lang)
2015/01/02 20:36:48
how about #intl.locale? or #IntlLocale ?
Alan Knight
2015/01/02 20:50:13
I had just assumed I couldn't use a period in a sy
| |
| 99 return zoneLocale == null ? _defaultLocale: zoneLocale; | |
| 100 } | |
| 101 static set defaultLocale(String newLocale) => _defaultLocale = newLocale; | |
| 102 static String _defaultLocale; | |
| 96 | 103 |
| 97 /** | 104 /** |
| 98 * The system's locale, as obtained from the window.navigator.language | 105 * The system's locale, as obtained from the window.navigator.language |
| 99 * or other operating system mechanism. Note that due to system limitations | 106 * or other operating system mechanism. Note that due to system limitations |
| 100 * this is not automatically set, and must be set by importing one of | 107 * this is not automatically set, and must be set by importing one of |
| 101 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). | 108 * intl_browser.dart or intl_standalone.dart and calling findSystemLocale(). |
| 102 */ | 109 */ |
| 103 static String systemLocale = 'en_US'; | 110 static String systemLocale = 'en_US'; |
| 104 | 111 |
| 105 /** | 112 /** |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 } | 341 } |
| 335 var exact = cases[choice]; | 342 var exact = cases[choice]; |
| 336 if (exact != null) return exact; | 343 if (exact != null) return exact; |
| 337 var other = cases["other"]; | 344 var other = cases["other"]; |
| 338 if (other == null) | 345 if (other == null) |
| 339 throw new ArgumentError("The 'other' case must be specified"); | 346 throw new ArgumentError("The 'other' case must be specified"); |
| 340 return other; | 347 return other; |
| 341 } | 348 } |
| 342 | 349 |
| 343 /** | 350 /** |
| 344 * Format the given function with a specific [locale], given a | 351 * Run [function] with the default locale set to [locale] and |
| 345 * [message_function] that takes no parameters. The [message_function] can be | 352 * return the result. |
| 346 * a simple message function that just returns the result of `Intl.message()` | |
| 347 * it can be a wrapper around a message function that takes arguments, or it | |
| 348 * can be something more complex that manipulates multiple message | |
| 349 * functions. | |
| 350 * | 353 * |
| 351 * In either case, the purpose of this is to delay calling [message_function] | 354 * This is run in a zone, so async operations invoked |
| 352 * until the proper locale has been set. This returns the result of calling | 355 * from within [message_function] will still have the locale set. |
| 353 * [message_function], which could be of an arbitrary type. | 356 * |
| 357 * The [function] might just returns the result of `Intl.message()` or | |
|
Siggi Cherem (dart-lang)
2015/01/02 20:36:48
rephrase this paragraph? (grammar seems a bit odd
Alan Knight
2015/01/02 20:50:13
Oops. Cleaned up, and removed some of the explanat
| |
| 358 * doing number or date formatting, | |
| 359 * it might be a wrapper around a message function that takes arguments, or it | |
| 360 * could be something more complex that invokes a number of formatting or | |
| 361 * message operations | |
| 362 * | |
| 363 * For example | |
| 364 * | |
| 365 * Intl.withLocale("fr", () => new NumberFormat.format(123456)); | |
| 366 * | |
| 367 * or | |
| 368 * | |
| 369 * hello(name) => Intl.message( | |
| 370 * "Hello $name.", | |
| 371 * name: 'hello', | |
| 372 * args: [name], | |
| 373 * desc: 'Say Hello'); | |
| 374 * Intl.withLocale("zh", new Timer(new Duration(milliseconds:10), | |
| 375 * () => print(hello("World"))); | |
| 354 */ | 376 */ |
| 355 static withLocale(String locale, Function message_function) { | 377 static withLocale(String locale, function()) { |
| 356 // We have to do this silliness because Locale is not known at compile time, | 378 var canonical = Intl.canonicalizedLocale(locale); |
| 357 // but must be a static variable in order to be visible to the Intl.message | 379 return runZoned(function, zoneValues: {#Intl_locale : canonical}); |
| 358 // invocation. | |
| 359 var oldLocale = getCurrentLocale(); | |
| 360 defaultLocale = Intl.canonicalizedLocale(locale); | |
| 361 var result = message_function(); | |
| 362 defaultLocale = oldLocale; | |
| 363 return result; | |
| 364 } | 380 } |
| 365 | 381 |
| 366 /** | 382 /** |
| 367 * Accessor for the current locale. This should always == the default locale, | 383 * Accessor for the current locale. This should always == the default locale, |
| 368 * unless for some reason this gets called inside a message that resets the | 384 * unless for some reason this gets called inside a message that resets the |
| 369 * locale. | 385 * locale. |
| 370 */ | 386 */ |
| 371 static String getCurrentLocale() { | 387 static String getCurrentLocale() { |
| 372 if (defaultLocale == null) defaultLocale = systemLocale; | 388 if (defaultLocale == null) defaultLocale = systemLocale; |
| 373 return defaultLocale; | 389 return defaultLocale; |
| 374 } | 390 } |
| 375 | 391 |
| 376 toString() => "Intl($locale)"; | 392 toString() => "Intl($locale)"; |
| 377 } | 393 } |
| OLD | NEW |