OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 // TODO(cira): Rename v8Locale into LocaleInfo once we have stable API. |
| 29 /** |
| 30 * LocaleInfo class is an aggregate class of all i18n API calls. |
| 31 * @param {Object} settings - localeID and regionID to create LocaleInfo from. |
| 32 * {Array.<string>|string} settings.localeID - |
| 33 * Unicode identifier of the locale. |
| 34 * See http://unicode.org/reports/tr35/#BCP_47_Conformance |
| 35 * {string} settings.regionID - ISO3166 region ID with addition of |
| 36 * invalid, undefined and reserved region codes. |
| 37 * @constructor |
| 38 */ |
| 39 v8Locale = function(settings) { |
| 40 native function NativeJSLocale(); |
| 41 |
| 42 // Assume user wanted to do v8Locale("sr"); |
| 43 if (typeof(settings) === "string") { |
| 44 settings = {'localeID': settings}; |
| 45 } |
| 46 |
| 47 var properties = NativeJSLocale( |
| 48 v8Locale.__createSettingsOrDefault(settings, {'localeID': 'root'})); |
| 49 |
| 50 // Keep the resolved ICU locale ID around to avoid resolving localeID to |
| 51 // ICU locale ID every time BreakIterator, Collator and so forth are called. |
| 52 this.__icuLocaleID = properties.icuLocaleID; |
| 53 this.options = {'localeID': properties.localeID, |
| 54 'regionID': properties.regionID}; |
| 55 }; |
| 56 |
| 57 /** |
| 58 * Clones existing locale with possible overrides for some of the options. |
| 59 * @param {!Object} settings - overrides for current locale settings. |
| 60 * @returns {Object} - new LocaleInfo object. |
| 61 */ |
| 62 v8Locale.prototype.derive = function(settings) { |
| 63 return new v8Locale( |
| 64 v8Locale.__createSettingsOrDefault(settings, this.options)); |
| 65 }; |
| 66 |
| 67 /** |
| 68 * v8BreakIterator class implements locale aware segmenatation. |
| 69 * It is not part of EcmaScript proposal. |
| 70 * @param {Object} locale - locale object to pass to break |
| 71 * iterator implementation. |
| 72 * @param {string} type - type of segmenatation: |
| 73 * - character |
| 74 * - word |
| 75 * - sentence |
| 76 * - line |
| 77 * @private |
| 78 * @constructor |
| 79 */ |
| 80 v8Locale.v8BreakIterator = function(locale, type) { |
| 81 native function NativeJSBreakIterator(); |
| 82 |
| 83 locale = v8Locale.__createLocaleOrDefault(locale); |
| 84 // BCP47 ID would work in this case, but we use ICU locale for consistency. |
| 85 var iterator = NativeJSBreakIterator(locale.__icuLocaleID, type); |
| 86 iterator.type = type; |
| 87 return iterator; |
| 88 }; |
| 89 |
| 90 /** |
| 91 * Type of the break we encountered during previous iteration. |
| 92 * @type{Enum} |
| 93 */ |
| 94 v8Locale.v8BreakIterator.BreakType = { |
| 95 'unknown': -1, |
| 96 'none': 0, |
| 97 'number': 100, |
| 98 'word': 200, |
| 99 'kana': 300, |
| 100 'ideo': 400 |
| 101 }; |
| 102 |
| 103 /** |
| 104 * Creates new v8BreakIterator based on current locale. |
| 105 * @param {string} - type of segmentation. See constructor. |
| 106 * @returns {Object} - new v8BreakIterator object. |
| 107 */ |
| 108 v8Locale.prototype.v8CreateBreakIterator = function(type) { |
| 109 return new v8Locale.v8BreakIterator(this, type); |
| 110 }; |
| 111 |
| 112 // TODO(jungshik): Set |collator.options| to actually recognized / resolved |
| 113 // values. |
| 114 /** |
| 115 * Collator class implements locale-aware sort. |
| 116 * @param {Object} locale - locale object to pass to collator implementation. |
| 117 * @param {Object} settings - collation flags: |
| 118 * - ignoreCase |
| 119 * - ignoreAccents |
| 120 * - numeric |
| 121 * @private |
| 122 * @constructor |
| 123 */ |
| 124 v8Locale.Collator = function(locale, settings) { |
| 125 native function NativeJSCollator(); |
| 126 |
| 127 locale = v8Locale.__createLocaleOrDefault(locale); |
| 128 var collator = NativeJSCollator( |
| 129 locale.__icuLocaleID, v8Locale.__createSettingsOrDefault(settings, {})); |
| 130 return collator; |
| 131 }; |
| 132 |
| 133 /** |
| 134 * Creates new Collator based on current locale. |
| 135 * @param {Object} - collation flags. See constructor. |
| 136 * @returns {Object} - new Collator object. |
| 137 */ |
| 138 v8Locale.prototype.createCollator = function(settings) { |
| 139 return new v8Locale.Collator(this, settings); |
| 140 }; |
| 141 |
| 142 /** |
| 143 * DateTimeFormat class implements locale-aware date and time formatting. |
| 144 * Constructor is not part of public API. |
| 145 * @param {Object} locale - locale object to pass to formatter. |
| 146 * @param {Object} settings - formatting flags: |
| 147 * - skeleton |
| 148 * - dateType |
| 149 * - timeType |
| 150 * - calendar |
| 151 * @private |
| 152 * @constructor |
| 153 */ |
| 154 v8Locale.__DateTimeFormat = function(locale, settings) { |
| 155 native function NativeJSDateTimeFormat(); |
| 156 |
| 157 settings = v8Locale.__createSettingsOrDefault(settings, {}); |
| 158 |
| 159 var cleanSettings = {}; |
| 160 if (settings.hasOwnProperty('skeleton')) { |
| 161 cleanSettings['skeleton'] = settings['skeleton']; |
| 162 } else { |
| 163 cleanSettings = {}; |
| 164 if (settings.hasOwnProperty('dateType')) { |
| 165 var dt = settings['dateType']; |
| 166 if (!/^short|medium|long|full$/.test(dt)) dt = 'short'; |
| 167 cleanSettings['dateType'] = dt; |
| 168 } |
| 169 |
| 170 if (settings.hasOwnProperty('timeType')) { |
| 171 var tt = settings['timeType']; |
| 172 if (!/^short|medium|long|full$/.test(tt)) tt = 'short'; |
| 173 cleanSettings['timeType'] = tt; |
| 174 } |
| 175 } |
| 176 |
| 177 // Default is to show short date and time. |
| 178 if (!cleanSettings.hasOwnProperty('skeleton') && |
| 179 !cleanSettings.hasOwnProperty('dateType') && |
| 180 !cleanSettings.hasOwnProperty('timeType')) { |
| 181 cleanSettings = {'dateType': 'short', |
| 182 'timeType': 'short'}; |
| 183 } |
| 184 |
| 185 locale = v8Locale.__createLocaleOrDefault(locale); |
| 186 var formatter = NativeJSDateTimeFormat(locale.__icuLocaleID, cleanSettings); |
| 187 |
| 188 // NativeJSDateTimeFormat creates formatter.options for us, we just need |
| 189 // to append actual settings to it. |
| 190 for (key in cleanSettings) { |
| 191 formatter.options[key] = cleanSettings[key]; |
| 192 } |
| 193 |
| 194 /** |
| 195 * Clones existing date time format with possible overrides for some |
| 196 * of the options. |
| 197 * @param {!Object} overrideSettings - overrides for current format settings. |
| 198 * @returns {Object} - new DateTimeFormat object. |
| 199 * @public |
| 200 */ |
| 201 formatter.derive = function(overrideSettings) { |
| 202 // To remove a setting user can specify undefined as its value. We'll remove |
| 203 // it from the map in that case. |
| 204 for (var prop in overrideSettings) { |
| 205 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) { |
| 206 delete settings[prop]; |
| 207 } |
| 208 } |
| 209 return new v8Locale.__DateTimeFormat( |
| 210 locale, v8Locale.__createSettingsOrDefault(overrideSettings, settings)); |
| 211 }; |
| 212 |
| 213 return formatter; |
| 214 }; |
| 215 |
| 216 /** |
| 217 * Creates new DateTimeFormat based on current locale. |
| 218 * @param {Object} - formatting flags. See constructor. |
| 219 * @returns {Object} - new DateTimeFormat object. |
| 220 */ |
| 221 v8Locale.prototype.createDateTimeFormat = function(settings) { |
| 222 return new v8Locale.__DateTimeFormat(this, settings); |
| 223 }; |
| 224 |
| 225 /** |
| 226 * Merges user settings and defaults. |
| 227 * Settings that are not of object type are rejected. |
| 228 * Actual property values are not validated, but whitespace is trimmed if they |
| 229 * are strings. |
| 230 * @param {!Object} settings - user provided settings. |
| 231 * @param {!Object} defaults - default values for this type of settings. |
| 232 * @returns {Object} - valid settings object. |
| 233 * @private |
| 234 */ |
| 235 v8Locale.__createSettingsOrDefault = function(settings, defaults) { |
| 236 if (!settings || typeof(settings) !== 'object' ) { |
| 237 return defaults; |
| 238 } |
| 239 for (var key in defaults) { |
| 240 if (!settings.hasOwnProperty(key)) { |
| 241 settings[key] = defaults[key]; |
| 242 } |
| 243 } |
| 244 // Clean up settings. |
| 245 for (var key in settings) { |
| 246 // Trim whitespace. |
| 247 if (typeof(settings[key]) === "string") { |
| 248 settings[key] = settings[key].trim(); |
| 249 } |
| 250 // Remove all properties that are set to undefined/null. This allows |
| 251 // derive method to remove a setting we don't need anymore. |
| 252 if (!settings[key]) { |
| 253 delete settings[key]; |
| 254 } |
| 255 } |
| 256 |
| 257 return settings; |
| 258 }; |
| 259 |
| 260 /** |
| 261 * If locale is valid (defined and of v8Locale type) we return it. If not |
| 262 * we create default locale and return it. |
| 263 * @param {!Object} locale - user provided locale. |
| 264 * @returns {Object} - v8Locale object. |
| 265 * @private |
| 266 */ |
| 267 v8Locale.__createLocaleOrDefault = function(locale) { |
| 268 if (!locale || !(locale instanceof v8Locale)) { |
| 269 return new v8Locale(); |
| 270 } else { |
| 271 return locale; |
| 272 } |
| 273 }; |
OLD | NEW |