OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #library('intl'); | |
6 | |
7 #import('dart:web'); | |
8 | |
9 #import('date_format.dart'); | |
10 #source('intl_message.dart'); | |
11 #source('bidi_formatter.dart'); | |
12 #source('bidi_utils.dart'); | |
13 | |
14 /** | |
15 * Internationalization object providing access to message formatting objects, | |
16 * date formatting, parsing, bidirectional text relative to a specific locale. | |
17 */ | |
18 | |
19 class Intl { | |
20 /** | |
21 * String indicating the locale code with which the message is to be | |
22 * formatted (such as en-CA). | |
23 */ | |
24 static String _locale; | |
25 | |
26 IntlMessage intlMsg; | |
27 | |
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 } | |
36 | |
37 /** | |
38 * Constructor optionally [_locale] for specifics of the language | |
39 * locale to be used, otherwise, we will attempt to infer it (acceptable if | |
40 * Dart is running on the client, we can infer from the browser/client | |
41 * preferences). | |
42 */ | |
43 Intl([a_locale]) { | |
44 if (a_locale == null) { | |
45 _locale = _getDefaultLocale(); | |
46 } else { | |
47 _locale = verifiedLocale(a_locale); | |
48 } | |
49 intlMsg = new IntlMessage(_locale); | |
50 } | |
51 | |
52 /** | |
53 * Create a message that can be internationalized. It takes a | |
54 * [message_str] that will be translated, which may be interpolated | |
55 * based on one or more variables, a [desc] providing a description of usage | |
56 * for the [message_str], and a map of [examples] for each data element to be | |
57 * substituted into the message. For example, if message="Hello, $name", then | |
58 * examples = {'name': 'Sparky'}. If not using the user's default locale, or | |
59 * if the locale is not easily detectable, explicitly pass [locale]. | |
60 * The values of [desc] and [examples] are not used at run-time but are only | |
61 * made available to the translators, so they MUST be simple Strings available | |
62 * at compile time: no String interpolation or concatenation. | |
63 * The expected usage of this is inside a function that takes as parameters | |
64 * the variables used in the interpolated string, and additionally also a | |
65 * locale (optional). | |
66 */ | |
67 static String message(String message_str, [final String desc='', | |
68 final Map examples=const {}, String locale='']) { | |
69 return message_str; | |
70 } | |
71 | |
72 /** | |
73 * Return the locale for this instance. If none was set, the locale will | |
74 * be the default. | |
75 */ | |
76 String get locale() => _locale; | |
77 | |
78 /** | |
79 * Return true if the locale exists, or if it is null. The null case | |
80 * is interpreted to mean that we use the default locale. | |
81 */ | |
82 static bool _localeExists(localeName) { | |
83 return DateFormat.localeExists(localeName); | |
84 } | |
85 | |
86 /** | |
87 * Given [newLocale] return a locale that we have data for that is similar | |
88 * to it, if possible. | |
89 * If [newLocale] is found directly, return it. If it can't be found, look up | |
90 * based on just the language (e.g. 'en_CA' -> 'en'). Also accepts '-' | |
91 * as a separator and changes it into '_' for lookup, and changes the | |
92 * country to uppercase. | |
93 * Note that null is interpreted as meaning the default locale, so if | |
94 * [newLocale] is null it will be returned. | |
95 */ | |
96 static String verifiedLocale(String newLocale) { | |
97 if (newLocale == null) return _getDefaultLocale(); | |
98 if (_localeExists(newLocale)) { | |
99 return newLocale; | |
100 } | |
101 for (var each in [_canonicalized(newLocale), _shortLocale(newLocale)]) { | |
102 if (_localeExists(each)) { | |
103 return each; | |
104 } | |
105 } | |
106 throw new IllegalArgumentException("Invalid locale '$newLocale'"); | |
107 } | |
108 | |
109 /** Return the short version of a locale name, e.g. 'en_US' => 'en' */ | |
110 static String _shortLocale(String aLocale) { | |
111 if (aLocale.length < 2) return aLocale; | |
112 return aLocale.substring(0, 2).toLowerCase(); | |
113 } | |
114 | |
115 /** | |
116 * Return a locale name turned into xx_YY where it might possibly be | |
117 * in the wrong case or with a hyphen instead of an underscore. | |
118 */ | |
119 static String _canonicalized(String aLocale) { | |
120 // Locales of length < 5 are presumably two-letter forms, or else malformed. | |
121 // Locales of length > 6 are likely to be malformed. In either case we | |
122 // return them unmodified and if correct they will be found. | |
123 if ((aLocale.length < 5) || (aLocale.length > 6)) return aLocale; | |
124 if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale; | |
125 return '${aLocale[0]}${aLocale[1]}_${aLocale[3].toUpperCase()}' | |
126 '${aLocale[4].toUpperCase()}'; | |
127 } | |
128 | |
129 /** | |
130 * Support method for message formatting. Select the correct plural form from | |
131 * [cases] given [howMany]. | |
132 */ | |
133 static String plural(var howMany, Map cases, [num offset=0]) { | |
134 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! | |
135 return select(howMany.toString(), cases); | |
136 } | |
137 | |
138 /** | |
139 * Format the given function with a specific [locale], given a | |
140 * [msg_function] that takes no parameters and returns a String. We | |
141 * basically delay calling the message function proper until after the proper | |
142 * locale has been set. | |
143 */ | |
144 static String withLocale(String locale, Function msg_function) { | |
145 // We have to do this silliness because Locale is not known at compile time, | |
146 // but must be a static variable. | |
147 if (_locale == null) _locale = _getDefaultLocale(); | |
148 var oldLocale = _locale; | |
149 _locale = locale; | |
150 var result = msg_function(); | |
151 _locale = oldLocale; | |
152 return result; | |
153 } | |
154 | |
155 /** | |
156 * Support method for message formatting. Select the correct exact (gender, | |
157 * usually) form from [cases] given the user [choice]. | |
158 */ | |
159 static String select(String choice, Map cases) { | |
160 if (cases.containsKey(choice)) { | |
161 return cases[choice]; | |
162 } else if (cases.containsKey('other')){ | |
163 return cases['other']; | |
164 } else { | |
165 return ''; | |
166 } | |
167 } | |
168 | |
169 /** | |
170 * Helper to detect the locale as defined at runtime. | |
171 */ | |
172 static String _getDefaultLocale() { | |
173 // TODO(efortuna): Detect the default locale given the user preferences. | |
174 // That would mean using window.navigator.language in a browser or | |
175 // an environment variable or other OS mechanism for the standalone VM. | |
176 // Yay, hard-coding for now! | |
177 return 'en_US'; | |
178 } | |
179 | |
180 /** | |
181 * Accessor for the current locale. This should always == the default locale, | |
182 * unless for some reason this gets called inside a message that resets the | |
183 * locale. | |
184 */ | |
185 static String getCurrentLocale() { | |
186 return _locale; | |
187 } | |
188 } | |
OLD | NEW |