Chromium Code Reviews| Index: lib/intl.dart |
| diff --git a/lib/intl.dart b/lib/intl.dart |
| index fcaed798b5ce4059d05adba0b9277b459092a789..5e5088d75745ac998ebb4ca4fb142cb5c50a25af 100644 |
| --- a/lib/intl.dart |
| +++ b/lib/intl.dart |
| @@ -20,6 +20,7 @@ |
| */ |
| library intl; |
| +import 'dart:async'; |
| import 'dart:collection'; |
| import 'dart:convert'; |
| import 'dart:math'; |
| @@ -88,11 +89,17 @@ class Intl { |
| */ |
| String _locale; |
| - /** The default locale. This defaults to being set from systemLocale, but |
| + /** |
| + * The default locale. This defaults to being set from systemLocale, but |
| * can also be set explicitly, and will then apply to any new instances where |
| * the locale isn't specified. |
| */ |
| - static String defaultLocale; |
| + static String get defaultLocale { |
| + var zoneLocale = Zone.current[#Intl.locale]; |
| + return zoneLocale == null ? _defaultLocale: zoneLocale; |
| + } |
| + static set defaultLocale(String newLocale) => _defaultLocale = newLocale; |
|
Sean Eagan
2015/01/02 21:05:30
Can this be deprecated now? It seems like `withLo
Alan Knight
2015/01/02 21:53:33
I think it's reasonable for the simple case that y
|
| + static String _defaultLocale; |
| /** |
| * The system's locale, as obtained from the window.navigator.language |
| @@ -341,26 +348,33 @@ class Intl { |
| } |
| /** |
| - * Format the given function with a specific [locale], given a |
| - * [message_function] that takes no parameters. The [message_function] can be |
| - * a simple message function that just returns the result of `Intl.message()` |
| - * it can be a wrapper around a message function that takes arguments, or it |
| - * can be something more complex that manipulates multiple message |
| - * functions. |
| + * Run [function] with the default locale set to [locale] and |
| + * return the result. |
| + * |
| + * This is run in a zone, so async operations invoked |
| + * from within [function] will still have the locale set. |
| + * |
| + * In simple usage [function] might be a single |
| + * `Intl.message()` call or number/date formatting operation. But it can |
| + * also be an arbitrary function that calls multiple Intl operations. |
| + * |
| + * For example |
| + * |
| + * Intl.withLocale("fr", () => new NumberFormat.format(123456)); |
| + * |
| + * or |
| * |
| - * In either case, the purpose of this is to delay calling [message_function] |
| - * until the proper locale has been set. This returns the result of calling |
| - * [message_function], which could be of an arbitrary type. |
| + * hello(name) => Intl.message( |
| + * "Hello $name.", |
| + * name: 'hello', |
| + * args: [name], |
| + * desc: 'Say Hello'); |
| + * Intl.withLocale("zh", new Timer(new Duration(milliseconds:10), |
| + * () => print(hello("World"))); |
| */ |
| - static withLocale(String locale, Function message_function) { |
| - // We have to do this silliness because Locale is not known at compile time, |
| - // but must be a static variable in order to be visible to the Intl.message |
| - // invocation. |
| - var oldLocale = getCurrentLocale(); |
| - defaultLocale = Intl.canonicalizedLocale(locale); |
| - var result = message_function(); |
| - defaultLocale = oldLocale; |
| - return result; |
| + static withLocale(String locale, function()) { |
| + var canonical = Intl.canonicalizedLocale(locale); |
| + return runZoned(function, zoneValues: {#Intl.locale : canonical}); |
| } |
| /** |