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

Unified Diff: lib/i18n/intl.dart

Issue 10807096: Add date formatting and parsing code. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 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
Index: lib/i18n/intl.dart
===================================================================
--- lib/i18n/intl.dart (revision 10253)
+++ lib/i18n/intl.dart (working copy)
@@ -2,22 +2,21 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+/**
+ * Internationalization object providing access to message formatting objects,
+ * date formatting, parsing, bidirectional text relative to a specific locale.
+ */
+
#library('intl');
Emily Fortuna 2012/08/06 22:43:00 personal preference, but I preferred the documenta
Alan Knight 2012/08/08 00:47:44 Done.
#import('dart:web');
+#import('date_format.dart');
#source('intl_message.dart');
-#source('date_format.dart');
#source('bidi_formatter.dart');
#source('bidi_utils.dart');
-/**
- * Internationalization object providing access to message formatting objects,
- * date formatting, parsing, bidirectional text relative to a specific locale.
- */
-
class Intl {
-
/**
* String indicating the locale code with which the message is to be
* formatted (such as en-CA).
@@ -26,7 +25,14 @@
IntlMessage intlMsg;
- DateFormat date;
+ /**
+ * Return a new date format using the specified [pattern].
+ * If [desiredLocale] is not specified, then we default to [locale].
+ */
+ DateFormat date(String pattern, [String desiredLocale]) {
+ var actualLocale = (desiredLocale == null) ? _locale : desiredLocale;
+ return new DateFormat(pattern,actualLocale);
Emily Fortuna 2012/08/06 22:43:00 space between parameters
Alan Knight 2012/08/08 00:47:44 Done.
+ }
/**
* Constructor optionally [_locale] for specifics of the language
@@ -35,10 +41,8 @@
* preferences).
*/
Intl([a_locale]) {
- _locale = a_locale;
- if (_locale == null) _locale = _getDefaultLocale();
+ _locale = (a_locale == null) ? _getDefaultLocale() : a_locale;
Emily Fortuna 2012/08/06 22:43:00 do we want to call "verifiedLocale" to canonicaliz
Alan Knight 2012/08/08 00:47:44 Good idea. Done.
intlMsg = new IntlMessage(_locale);
- date = new DateFormat(_locale);
}
/**
@@ -61,7 +65,72 @@
return message_str;
}
+ /** The default locale for this environment. */
+ // TODO(alanknight): Return the correct locale using either
+ // window.navigator.language or appropriate environment variable or other
+ // mechanism for command-line apps.
+ static String defaultLocale = 'en_US';
Emily Fortuna 2012/08/06 22:43:00 let's move this down to the _getDefaultLocale func
+
/**
+ * Return the locale for this instance. If none was set, returns the
+ * default.
+ */
+ String get locale() {
+ if (_locale == null) {
Emily Fortuna 2012/08/06 22:43:00 Since we do this check when Intl is created, do we
+ return defaultLocale;
+ } else {
+ return _locale;}
+ }
+
+ /**
+ * Return true if the locale exists, or if it is null. The null case
+ * is interpreted to mean that we use the default locale.
+ */
+ static bool _localeExists(localeName) {
+ return DateFormat.localeExists(localeName);
+ }
+
+ /**
+ * Given [newLocale] return a locale that we have data for that is similar
+ * to it, if possible.
+ * If [newLocale] is found directly, return it. If it can't be found, look up
+ * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-'
+ * as a separator and changes it into '_' for lookup, and changes the
+ * country to uppercase.
+ * Note that null is interpreted as meaning the default locale, so if
+ * [newLocale] is null it will be returned.
+ */
+ static String verifiedLocale(String newLocale) {
+ if (newLocale == null) return defaultLocale;
Emily Fortuna 2012/08/06 22:43:00 call _getDefaultLocale...
+ if (_localeExists(newLocale)) {
+ return newLocale;
+ }
+ for (var each in [_canonicalized(newLocale), _shortLocale(newLocale)]) {
+ if (_localeExists(each)) {
+ return each;
+ }
+ }
+ throw new IllegalArgumentException("Invalid locale '$newLocale'");
+ }
+
+ /** Return the short version of a locale name, e.g. 'en_US' => 'en' */
+ static String _shortLocale(String aLocale) {
+ if (aLocale.length < 2) return aLocale;
+ return aLocale.substring(0,2).toLowerCase();
+ }
+
+ /**
+ * Return a locale name turned into xx_YY where it might possibly be
+ * in the wrong case or with a hyphen instead of an underscore.
+ */
+ static String _canonicalized(String aLocale) {
+ if (aLocale.length != 5) return aLocale;
Emily Fortuna 2012/08/06 22:43:00 Consider rewriting. It feels a little misleading t
Alan Knight 2012/08/08 00:47:44 I think I'd rather not throw the exception here be
Emily Fortuna 2012/08/08 01:35:54 Okay. Can you just put a comment maybe to the effe
Alan Knight 2012/08/08 18:21:25 Done. The es_419 case also points out that length
+ if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale;
+ return '${aLocale[0]}${aLocale[1]}_${aLocale[3].toUpperCase()}'
+ '${aLocale[4].toUpperCase()}';
+ }
+
+ /**
* Support method for message formatting. Select the correct plural form from
* [cases] given [howMany].
*/

Powered by Google App Engine
This is Rietveld 408576698