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

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'); 5 #library('intl');
6 6
7 #import('dart:web'); 7 #import('dart:web');
8 8
9 #import('date_format.dart');
9 #source('intl_message.dart'); 10 #source('intl_message.dart');
10 #source('date_format.dart');
11 #source('bidi_formatter.dart'); 11 #source('bidi_formatter.dart');
12 #source('bidi_utils.dart'); 12 #source('bidi_utils.dart');
13 13
14 /** 14 /**
15 * Internationalization object providing access to message formatting objects, 15 * Internationalization object providing access to message formatting objects,
16 * date formatting, parsing, bidirectional text relative to a specific locale. 16 * date formatting, parsing, bidirectional text relative to a specific locale.
17 */ 17 */
18 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);
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)
39 if (_locale == null) _locale = _getDefaultLocale(); 45 ? _getDefaultLocale() : verifiedLocale(a_locale);
Emily Fortuna 2012/08/08 19:27:36 If the ? : spans more than one line, switch to if/
Alan Knight 2012/08/08 19:39:57 Done.
40 intlMsg = new IntlMessage(_locale); 46 intlMsg = new IntlMessage(_locale);
41 date = new DateFormat(_locale);
42 } 47 }
43 48
44 /** 49 /**
45 * Create a message that can be internationalized. It takes a 50 * Create a message that can be internationalized. It takes a
46 * [message_str] that will be translated, which may be interpolated 51 * [message_str] that will be translated, which may be interpolated
47 * based on one or more variables, a [desc] providing a description of usage 52 * 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 53 * 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 54 * substituted into the message. For example, if message="Hello, $name", then
50 * examples = {'name': 'Sparky'}. If not using the user's default locale, or 55 * examples = {'name': 'Sparky'}. If not using the user's default locale, or
51 * if the locale is not easily detectable, explicitly pass [locale]. 56 * 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 57 * 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 58 * made available to the translators, so they MUST be simple Strings available
54 * at compile time: no String interpolation or concatenation. 59 * at compile time: no String interpolation or concatenation.
55 * The expected usage of this is inside a function that takes as parameters 60 * 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 61 * the variables used in the interpolated string, and additionally also a
57 * locale (optional). 62 * locale (optional).
58 */ 63 */
59 static String message(String message_str, [final String desc='', 64 static String message(String message_str, [final String desc='',
60 final Map examples=const {}, String locale='']) { 65 final Map examples=const {}, String locale='']) {
61 return message_str; 66 return message_str;
62 } 67 }
63 68
64 /** 69 /**
70 * Return the locale for this instance. If none was set, the locale will
71 * be the default.
72 */
73 String get locale() => _locale;
74
75 /**
76 * Return true if the locale exists, or if it is null. The null case
77 * is interpreted to mean that we use the default locale.
78 */
79 static bool _localeExists(localeName) {
80 return DateFormat.localeExists(localeName);
81 }
82
83 /**
84 * Given [newLocale] return a locale that we have data for that is similar
85 * to it, if possible.
86 * If [newLocale] is found directly, return it. If it can't be found, look up
87 * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-'
88 * as a separator and changes it into '_' for lookup, and changes the
89 * country to uppercase.
90 * Note that null is interpreted as meaning the default locale, so if
91 * [newLocale] is null it will be returned.
92 */
93 static String verifiedLocale(String newLocale) {
94 if (newLocale == null) return _getDefaultLocale();
95 if (_localeExists(newLocale)) {
96 return newLocale;
97 }
98 for (var each in [_canonicalized(newLocale), _shortLocale(newLocale)]) {
99 if (_localeExists(each)) {
100 return each;
101 }
102 }
103 throw new IllegalArgumentException("Invalid locale '$newLocale'");
104 }
105
106 /** Return the short version of a locale name, e.g. 'en_US' => 'en' */
107 static String _shortLocale(String aLocale) {
108 if (aLocale.length < 2) return aLocale;
109 return aLocale.substring(0,2).toLowerCase();
110 }
111
112 /**
113 * Return a locale name turned into xx_YY where it might possibly be
114 * in the wrong case or with a hyphen instead of an underscore.
115 */
116 static String _canonicalized(String aLocale) {
117 if (aLocale.length != 5) return aLocale;
118 if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale;
119 return '${aLocale[0]}${aLocale[1]}_${aLocale[3].toUpperCase()}'
120 '${aLocale[4].toUpperCase()}';
121 }
122
123 /**
65 * Support method for message formatting. Select the correct plural form from 124 * Support method for message formatting. Select the correct plural form from
66 * [cases] given [howMany]. 125 * [cases] given [howMany].
67 */ 126 */
68 static String plural(var howMany, Map cases, [num offset=0]) { 127 static String plural(var howMany, Map cases, [num offset=0]) {
69 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! 128 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others!
70 return select(howMany.toString(), cases); 129 return select(howMany.toString(), cases);
71 } 130 }
72 131
73 /** 132 /**
74 * Format the given function with a specific [locale], given a 133 * Format the given function with a specific [locale], given a
(...skipping 24 matching lines...) Expand all
99 } else { 158 } else {
100 return ''; 159 return '';
101 } 160 }
102 } 161 }
103 162
104 /** 163 /**
105 * Helper to detect the locale as defined at runtime. 164 * Helper to detect the locale as defined at runtime.
106 */ 165 */
107 static String _getDefaultLocale() { 166 static String _getDefaultLocale() {
108 // TODO(efortuna): Detect the default locale given the user preferences. 167 // TODO(efortuna): Detect the default locale given the user preferences.
168 // That would mean using window.navigator.language in a browser or
169 // an environment variable or other OS mechanism for the standalone VM.
109 // Yay, hard-coding for now! 170 // Yay, hard-coding for now!
110 return 'en-US'; 171 return 'en_US';
111 } 172 }
112 173
113 /** 174 /**
114 * Accessor for the current locale. This should always == the default locale, 175 * 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 176 * unless for some reason this gets called inside a message that resets the
116 * locale. 177 * locale.
117 */ 178 */
118 static String getCurrentLocale() { 179 static String getCurrentLocale() {
119 return _locale; 180 return _locale;
120 } 181 }
121 } 182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698