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

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: Mention that defaultLocale is superceded by withLocale 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
« no previous file with comments | « README.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..b883f0a02c791826286cedfd9bc3a355d95397db 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,21 @@ 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.
+ * the locale isn't specified. Note that a locale parameter to
+ * [Intl.withLocale]
+ * will supercede this value while that operation is active. Using
+ * [Intl.withLocale] may be preferable if you are using different locales
+ * in the same application.
*/
- 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;
+ static String _defaultLocale;
/**
* The system's locale, as obtained from the window.navigator.language
@@ -341,26 +352,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});
}
/**
« no previous file with comments | « README.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698