Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Unified Diff: lib/intl.dart

Issue 832073002: Make withLocale use a zone (Closed) Base URL: https://github.com/dart-lang/intl.git@master
Patch Set: Update CHANGELOG and pubspec to bump version Created 5 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« CHANGELOG.md ('K') | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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});
}
/**
« CHANGELOG.md ('K') | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698