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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #library('intl');
6
7 #import('dart:web');
8
9 #source('intl_message.dart');
10 #source('date_format.dart');
11 #source('bidi_formatter.dart');
12 #source('bidi_utils.dart');
13
14 /** 5 /**
15 * Internationalization object providing access to message formatting objects, 6 * Internationalization object providing access to message formatting objects,
16 * date formatting, parsing, bidirectional text relative to a specific locale. 7 * date formatting, parsing, bidirectional text relative to a specific locale.
17 */ 8 */
18 9
10 #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.
11
12 #import('dart:web');
13
14 #import('date_format.dart');
15 #source('intl_message.dart');
16 #source('bidi_formatter.dart');
17 #source('bidi_utils.dart');
18
19 class Intl { 19 class Intl {
20
21 /** 20 /**
22 * String indicating the locale code with which the message is to be 21 * String indicating the locale code with which the message is to be
23 * formatted (such as en-CA). 22 * formatted (such as en-CA).
24 */ 23 */
25 static String _locale; 24 static String _locale;
26 25
27 IntlMessage intlMsg; 26 IntlMessage intlMsg;
28 27
29 DateFormat date; 28 /**
29 * Return a new date format using the specified [pattern].
30 * If [desiredLocale] is not specified, then we default to [locale].
31 */
32 DateFormat date(String pattern, [String desiredLocale]) {
33 var actualLocale = (desiredLocale == null) ? _locale : desiredLocale;
34 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.
35 }
30 36
31 /** 37 /**
32 * Constructor optionally [_locale] for specifics of the language 38 * Constructor optionally [_locale] for specifics of the language
33 * locale to be used, otherwise, we will attempt to infer it (acceptable if 39 * locale to be used, otherwise, we will attempt to infer it (acceptable if
34 * Dart is running on the client, we can infer from the browser/client 40 * Dart is running on the client, we can infer from the browser/client
35 * preferences). 41 * preferences).
36 */ 42 */
37 Intl([a_locale]) { 43 Intl([a_locale]) {
38 _locale = a_locale; 44 _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.
39 if (_locale == null) _locale = _getDefaultLocale();
40 intlMsg = new IntlMessage(_locale); 45 intlMsg = new IntlMessage(_locale);
41 date = new DateFormat(_locale);
42 } 46 }
43 47
44 /** 48 /**
45 * Create a message that can be internationalized. It takes a 49 * Create a message that can be internationalized. It takes a
46 * [message_str] that will be translated, which may be interpolated 50 * [message_str] that will be translated, which may be interpolated
47 * based on one or more variables, a [desc] providing a description of usage 51 * based on one or more variables, a [desc] providing a description of usage
48 * for the [message_str], and a map of [examples] for each data element to be 52 * for the [message_str], and a map of [examples] for each data element to be
49 * substituted into the message. For example, if message="Hello, $name", then 53 * substituted into the message. For example, if message="Hello, $name", then
50 * examples = {'name': 'Sparky'}. If not using the user's default locale, or 54 * examples = {'name': 'Sparky'}. If not using the user's default locale, or
51 * if the locale is not easily detectable, explicitly pass [locale]. 55 * if the locale is not easily detectable, explicitly pass [locale].
52 * The values of [desc] and [examples] are not used at run-time but are only 56 * The values of [desc] and [examples] are not used at run-time but are only
53 * made available to the translators, so they MUST be simple Strings available 57 * made available to the translators, so they MUST be simple Strings available
54 * at compile time: no String interpolation or concatenation. 58 * at compile time: no String interpolation or concatenation.
55 * The expected usage of this is inside a function that takes as parameters 59 * The expected usage of this is inside a function that takes as parameters
56 * the variables used in the interpolated string, and additionally also a 60 * the variables used in the interpolated string, and additionally also a
57 * locale (optional). 61 * locale (optional).
58 */ 62 */
59 static String message(String message_str, [final String desc='', 63 static String message(String message_str, [final String desc='',
60 final Map examples=const {}, String locale='']) { 64 final Map examples=const {}, String locale='']) {
61 return message_str; 65 return message_str;
62 } 66 }
63 67
68 /** The default locale for this environment. */
69 // TODO(alanknight): Return the correct locale using either
70 // window.navigator.language or appropriate environment variable or other
71 // mechanism for command-line apps.
72 static String defaultLocale = 'en_US';
Emily Fortuna 2012/08/06 22:43:00 let's move this down to the _getDefaultLocale func
73
74 /**
75 * Return the locale for this instance. If none was set, returns the
76 * default.
77 */
78 String get locale() {
79 if (_locale == null) {
Emily Fortuna 2012/08/06 22:43:00 Since we do this check when Intl is created, do we
80 return defaultLocale;
81 } else {
82 return _locale;}
83 }
84
85 /**
86 * Return true if the locale exists, or if it is null. The null case
87 * is interpreted to mean that we use the default locale.
88 */
89 static bool _localeExists(localeName) {
90 return DateFormat.localeExists(localeName);
91 }
92
93 /**
94 * Given [newLocale] return a locale that we have data for that is similar
95 * to it, if possible.
96 * If [newLocale] is found directly, return it. If it can't be found, look up
97 * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-'
98 * as a separator and changes it into '_' for lookup, and changes the
99 * country to uppercase.
100 * Note that null is interpreted as meaning the default locale, so if
101 * [newLocale] is null it will be returned.
102 */
103 static String verifiedLocale(String newLocale) {
104 if (newLocale == null) return defaultLocale;
Emily Fortuna 2012/08/06 22:43:00 call _getDefaultLocale...
105 if (_localeExists(newLocale)) {
106 return newLocale;
107 }
108 for (var each in [_canonicalized(newLocale), _shortLocale(newLocale)]) {
109 if (_localeExists(each)) {
110 return each;
111 }
112 }
113 throw new IllegalArgumentException("Invalid locale '$newLocale'");
114 }
115
116 /** Return the short version of a locale name, e.g. 'en_US' => 'en' */
117 static String _shortLocale(String aLocale) {
118 if (aLocale.length < 2) return aLocale;
119 return aLocale.substring(0,2).toLowerCase();
120 }
121
122 /**
123 * Return a locale name turned into xx_YY where it might possibly be
124 * in the wrong case or with a hyphen instead of an underscore.
125 */
126 static String _canonicalized(String aLocale) {
127 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
128 if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale;
129 return '${aLocale[0]}${aLocale[1]}_${aLocale[3].toUpperCase()}'
130 '${aLocale[4].toUpperCase()}';
131 }
132
64 /** 133 /**
65 * Support method for message formatting. Select the correct plural form from 134 * Support method for message formatting. Select the correct plural form from
66 * [cases] given [howMany]. 135 * [cases] given [howMany].
67 */ 136 */
68 static String plural(var howMany, Map cases, [num offset=0]) { 137 static String plural(var howMany, Map cases, [num offset=0]) {
69 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! 138 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others!
70 return select(howMany.toString(), cases); 139 return select(howMany.toString(), cases);
71 } 140 }
72 141
73 /** 142 /**
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 181
113 /** 182 /**
114 * Accessor for the current locale. This should always == the default locale, 183 * Accessor for the current locale. This should always == the default locale,
115 * unless for some reason this gets called inside a message that resets the 184 * unless for some reason this gets called inside a message that resets the
116 * locale. 185 * locale.
117 */ 186 */
118 static String getCurrentLocale() { 187 static String getCurrentLocale() {
119 return _locale; 188 return _locale;
120 } 189 }
121 } 190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698