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