Index: lib/intl.dart |
diff --git a/lib/intl.dart b/lib/intl.dart |
index fcaed798b5ce4059d05adba0b9277b459092a789..3165bd998191081781d0ac44bd16ae777bfae883 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]; |
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
|
+ return zoneLocale == null ? _defaultLocale: zoneLocale; |
+ } |
+ static set defaultLocale(String newLocale) => _defaultLocale = newLocale; |
+ static String _defaultLocale; |
/** |
* The system's locale, as obtained from the window.navigator.language |
@@ -341,26 +348,35 @@ 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 [message_function] will still have the locale set. |
+ * |
+ * 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
|
+ * doing number or date formatting, |
+ * it might be a wrapper around a message function that takes arguments, or it |
+ * could be something more complex that invokes a number of formatting or |
+ * message 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}); |
} |
/** |