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

Unified Diff: lib/i18n/intl.dart

Issue 10536105: Make Intl.message static (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 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 | « no previous file | lib/i18n/intl_message.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/i18n/intl.dart
===================================================================
--- lib/i18n/intl.dart (revision 8515)
+++ lib/i18n/intl.dart (working copy)
@@ -18,7 +18,7 @@
* String indicating the locale code with which the message is to be
* formatted (such as en-CA).
*/
- String _locale;
+ static String _locale = _getDefaultLocale();
Alan Knight 2012/06/12 17:47:20 I think this would have to change in line with the
IntlMessage intlMsg;
@@ -41,15 +41,17 @@
* based on one or more variables, a [desc] providing a description of usage
* for the [message_str], and a map of [examples] for each data element to be
* substituted into the message. For example, if message="Hello, $name", then
- * examples = {'name': 'Sparky'}. The values of [desc] and [examples] are
- * not used at run-time but are only made available to the translators, so
- * they MUST be simple Strings available at compile time: no String
- * interpolation or concatenation.
+ * examples = {'name': 'Sparky'}. If not using the user's default locale, or
+ * if the locale is not easily detectable, explicitly pass [locale].
+ * The values of [desc] and [examples] are not used at run-time but are only
+ * made available to the translators, so they MUST be simple Strings available
+ * at compile time: no String interpolation or concatenation.
* The expected usage of this is inside a function that takes as parameters
- * the variables used in the interpolated string.
+ * the variables used in the interpolated string, and additionally also a
+ * locale (optional).
*/
- String message(String message_str, [final String desc='',
- final Map examples=const {}]) {
+ static String message(String message_str, [final String desc='',
+ final Map examples=const {}, String locale='']) {
Alan Knight 2012/06/12 17:47:20 Does it make sense now to have the locale paramete
return message_str;
}
@@ -59,12 +61,22 @@
*/
static String plural(var howMany, Map cases, [num offset=0]) {
// TODO(efortuna): Deal with "few" and "many" cases, offset, and others!
- // TODO(alanknight): Should we have instance methods instead/as well?
- // Or have the others as statics?
return select(howMany.toString(), cases);
}
/**
+ * Format the given function with a specific [locale], given a [msg_function]
+ * that takes no parameters and returns a String.
+ */
+ static String withLocale(String locale, Function msg_function) {
+ var oldLocale = _locale;
+ _locale = locale;
+ var result = msg_function();
+ _locale = oldLocale;
+ return result;
+ }
+
+ /**
* Support method for message formatting. Select the correct exact (gender,
* usually) form from [cases] given the user [choice].
*/
@@ -77,4 +89,13 @@
return '';
}
}
+
+ /**
+ * Helper to detect the locale as defined at runtime.
+ */
+ static String _getDefaultLocale() {
+ // TODO(efortuna): Detect the default locale given the user preferences.
+ // Yay, hard-coding for now!
+ return 'en-US';
Alan Knight 2012/06/12 17:47:20 Doesn't this need to be if (!(_locale == null))
+ }
}
« no previous file with comments | « no previous file | lib/i18n/intl_message.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698