| OLD | NEW |
| 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 if (a_locale == null) { |
| 39 if (_locale == null) _locale = _getDefaultLocale(); | 45 _locale = _getDefaultLocale(); |
| 46 } else { |
| 47 _locale = verifiedLocale(a_locale); |
| 48 } |
| 40 intlMsg = new IntlMessage(_locale); | 49 intlMsg = new IntlMessage(_locale); |
| 41 date = new DateFormat(_locale); | |
| 42 } | 50 } |
| 43 | 51 |
| 44 /** | 52 /** |
| 45 * Create a message that can be internationalized. It takes a | 53 * Create a message that can be internationalized. It takes a |
| 46 * [message_str] that will be translated, which may be interpolated | 54 * [message_str] that will be translated, which may be interpolated |
| 47 * based on one or more variables, a [desc] providing a description of usage | 55 * 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 | 56 * 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 | 57 * substituted into the message. For example, if message="Hello, $name", then |
| 50 * examples = {'name': 'Sparky'}. If not using the user's default locale, or | 58 * examples = {'name': 'Sparky'}. If not using the user's default locale, or |
| 51 * if the locale is not easily detectable, explicitly pass [locale]. | 59 * 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 | 60 * 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 | 61 * made available to the translators, so they MUST be simple Strings available |
| 54 * at compile time: no String interpolation or concatenation. | 62 * at compile time: no String interpolation or concatenation. |
| 55 * The expected usage of this is inside a function that takes as parameters | 63 * 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 | 64 * the variables used in the interpolated string, and additionally also a |
| 57 * locale (optional). | 65 * locale (optional). |
| 58 */ | 66 */ |
| 59 static String message(String message_str, [final String desc='', | 67 static String message(String message_str, [final String desc='', |
| 60 final Map examples=const {}, String locale='']) { | 68 final Map examples=const {}, String locale='']) { |
| 61 return message_str; | 69 return message_str; |
| 62 } | 70 } |
| 63 | 71 |
| 64 /** | 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 /** |
| 65 * Support method for message formatting. Select the correct plural form from | 130 * Support method for message formatting. Select the correct plural form from |
| 66 * [cases] given [howMany]. | 131 * [cases] given [howMany]. |
| 67 */ | 132 */ |
| 68 static String plural(var howMany, Map cases, [num offset=0]) { | 133 static String plural(var howMany, Map cases, [num offset=0]) { |
| 69 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! | 134 // TODO(efortuna): Deal with "few" and "many" cases, offset, and others! |
| 70 return select(howMany.toString(), cases); | 135 return select(howMany.toString(), cases); |
| 71 } | 136 } |
| 72 | 137 |
| 73 /** | 138 /** |
| 74 * Format the given function with a specific [locale], given a | 139 * Format the given function with a specific [locale], given a |
| (...skipping 24 matching lines...) Expand all Loading... |
| 99 } else { | 164 } else { |
| 100 return ''; | 165 return ''; |
| 101 } | 166 } |
| 102 } | 167 } |
| 103 | 168 |
| 104 /** | 169 /** |
| 105 * Helper to detect the locale as defined at runtime. | 170 * Helper to detect the locale as defined at runtime. |
| 106 */ | 171 */ |
| 107 static String _getDefaultLocale() { | 172 static String _getDefaultLocale() { |
| 108 // TODO(efortuna): Detect the default locale given the user preferences. | 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. |
| 109 // Yay, hard-coding for now! | 176 // Yay, hard-coding for now! |
| 110 return 'en-US'; | 177 return 'en_US'; |
| 111 } | 178 } |
| 112 | 179 |
| 113 /** | 180 /** |
| 114 * Accessor for the current locale. This should always == the default locale, | 181 * 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 | 182 * unless for some reason this gets called inside a message that resets the |
| 116 * locale. | 183 * locale. |
| 117 */ | 184 */ |
| 118 static String getCurrentLocale() { | 185 static String getCurrentLocale() { |
| 119 return _locale; | 186 return _locale; |
| 120 } | 187 } |
| 121 } | 188 } |
| OLD | NEW |