| OLD | NEW |
| 1 // Copyright 2006-2011 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // TODO(cira): Rename v8Locale into LocaleInfo once we have stable API. | 28 // TODO(cira): Remove v8 prefix from v8Locale once we have stable API. |
| 29 /** | 29 v8Locale = function(optLocale) { |
| 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(); | 30 native function NativeJSLocale(); |
| 41 | 31 var properties = NativeJSLocale(optLocale); |
| 42 // Assume user wanted to do v8Locale("sr"); | 32 this.locale = properties.locale; |
| 43 if (typeof(settings) === "string") { | 33 this.language = properties.language; |
| 44 settings = {'localeID': settings}; | 34 this.script = properties.script; |
| 45 } | 35 this.region = properties.region; |
| 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 }; | 36 }; |
| 56 | 37 |
| 57 /** | 38 v8Locale.availableLocales = function() { |
| 58 * Clones existing locale with possible overrides for some of the options. | 39 native function NativeJSAvailableLocales(); |
| 59 * @param {!Object} settings - overrides for current locale settings. | 40 return NativeJSAvailableLocales(); |
| 60 * @returns {Object} - new LocaleInfo object. | |
| 61 */ | |
| 62 v8Locale.prototype.derive = function(settings) { | |
| 63 return new v8Locale( | |
| 64 v8Locale.createSettingsOrDefault_(settings, this.options)); | |
| 65 }; | 41 }; |
| 66 | 42 |
| 67 /** | 43 v8Locale.prototype.maximizedLocale = function() { |
| 68 * v8BreakIterator class implements locale aware segmenatation. | 44 native function NativeJSMaximizedLocale(); |
| 69 * It is not part of EcmaScript proposal. | 45 return new v8Locale(NativeJSMaximizedLocale(this.locale)); |
| 70 * @param {Object} locale - locale object to pass to break | 46 }; |
| 71 * iterator implementation. | 47 |
| 72 * @param {string} type - type of segmenatation: | 48 v8Locale.prototype.minimizedLocale = function() { |
| 73 * - character | 49 native function NativeJSMinimizedLocale(); |
| 74 * - word | 50 return new v8Locale(NativeJSMinimizedLocale(this.locale)); |
| 75 * - sentence | 51 }; |
| 76 * - line | 52 |
| 77 * @constructor | 53 v8Locale.prototype.displayLocale_ = function(displayLocale) { |
| 78 */ | 54 var result = this.locale; |
| 55 if (displayLocale !== undefined) { |
| 56 result = displayLocale.locale; |
| 57 } |
| 58 return result; |
| 59 }; |
| 60 |
| 61 v8Locale.prototype.displayLanguage = function(optDisplayLocale) { |
| 62 var displayLocale = this.displayLocale_(optDisplayLocale); |
| 63 native function NativeJSDisplayLanguage(); |
| 64 return NativeJSDisplayLanguage(this.locale, displayLocale); |
| 65 }; |
| 66 |
| 67 v8Locale.prototype.displayScript = function(optDisplayLocale) { |
| 68 var displayLocale = this.displayLocale_(optDisplayLocale); |
| 69 native function NativeJSDisplayScript(); |
| 70 return NativeJSDisplayScript(this.locale, displayLocale); |
| 71 }; |
| 72 |
| 73 v8Locale.prototype.displayRegion = function(optDisplayLocale) { |
| 74 var displayLocale = this.displayLocale_(optDisplayLocale); |
| 75 native function NativeJSDisplayRegion(); |
| 76 return NativeJSDisplayRegion(this.locale, displayLocale); |
| 77 }; |
| 78 |
| 79 v8Locale.prototype.displayName = function(optDisplayLocale) { |
| 80 var displayLocale = this.displayLocale_(optDisplayLocale); |
| 81 native function NativeJSDisplayName(); |
| 82 return NativeJSDisplayName(this.locale, displayLocale); |
| 83 }; |
| 84 |
| 79 v8Locale.v8BreakIterator = function(locale, type) { | 85 v8Locale.v8BreakIterator = function(locale, type) { |
| 80 native function NativeJSBreakIterator(); | 86 native function NativeJSBreakIterator(); |
| 81 | 87 var iterator = NativeJSBreakIterator(locale, type); |
| 82 locale = v8Locale.createLocaleOrDefault_(locale); | |
| 83 // BCP47 ID would work in this case, but we use ICU locale for consistency. | |
| 84 var iterator = NativeJSBreakIterator(locale.icuLocaleID_, type); | |
| 85 iterator.type = type; | 88 iterator.type = type; |
| 86 return iterator; | 89 return iterator; |
| 87 }; | 90 }; |
| 88 | 91 |
| 89 /** | |
| 90 * Type of the break we encountered during previous iteration. | |
| 91 * @type{Enum} | |
| 92 */ | |
| 93 v8Locale.v8BreakIterator.BreakType = { | 92 v8Locale.v8BreakIterator.BreakType = { |
| 94 'unknown': -1, | 93 'unknown': -1, |
| 95 'none': 0, | 94 'none': 0, |
| 96 'number': 100, | 95 'number': 100, |
| 97 'word': 200, | 96 'word': 200, |
| 98 'kana': 300, | 97 'kana': 300, |
| 99 'ideo': 400 | 98 'ideo': 400 |
| 100 }; | 99 }; |
| 101 | 100 |
| 102 /** | |
| 103 * Creates new v8BreakIterator based on current locale. | |
| 104 * @param {string} - type of segmentation. See constructor. | |
| 105 * @returns {Object} - new v8BreakIterator object. | |
| 106 */ | |
| 107 v8Locale.prototype.v8CreateBreakIterator = function(type) { | 101 v8Locale.prototype.v8CreateBreakIterator = function(type) { |
| 108 return new v8Locale.v8BreakIterator(this, type); | 102 return new v8Locale.v8BreakIterator(this.locale, type); |
| 109 }; | 103 }; |
| 110 | 104 |
| 111 // TODO(jungshik): Set |collator.options| to actually recognized / resolved | 105 // TODO(jungshik): Set |collator.options| to actually recognized / resolved |
| 112 // values. | 106 // values. |
| 113 /** | 107 v8Locale.Collator = function(locale, options) { |
| 114 * Collator class implements locale-aware sort. | |
| 115 * @param {Object} locale - locale object to pass to collator implementation. | |
| 116 * @param {Object} settings - collation flags: | |
| 117 * - ignoreCase | |
| 118 * - ignoreAccents | |
| 119 * - numeric | |
| 120 * @constructor | |
| 121 */ | |
| 122 v8Locale.Collator = function(locale, settings) { | |
| 123 native function NativeJSCollator(); | 108 native function NativeJSCollator(); |
| 124 | 109 var collator = NativeJSCollator(locale, |
| 125 locale = v8Locale.createLocaleOrDefault_(locale); | 110 options === undefined ? {} : options); |
| 126 var collator = NativeJSCollator( | |
| 127 locale.icuLocaleID_, v8Locale.createSettingsOrDefault_(settings, {})); | |
| 128 return collator; | 111 return collator; |
| 129 }; | 112 }; |
| 130 | 113 |
| 131 /** | 114 v8Locale.prototype.createCollator = function(options) { |
| 132 * Creates new Collator based on current locale. | 115 return new v8Locale.Collator(this.locale, options); |
| 133 * @param {Object} - collation flags. See constructor. | |
| 134 * @returns {Object} - new v8BreakIterator object. | |
| 135 */ | |
| 136 v8Locale.prototype.createCollator = function(settings) { | |
| 137 return new v8Locale.Collator(this, settings); | |
| 138 }; | 116 }; |
| 139 | |
| 140 /** | |
| 141 * Merges user settings and defaults. | |
| 142 * Settings that are not of object type are rejected. | |
| 143 * Actual property values are not validated, but whitespace is trimmed if they | |
| 144 * are strings. | |
| 145 * @param {!Object} settings - user provided settings. | |
| 146 * @param {!Object} defaults - default values for this type of settings. | |
| 147 * @returns {Object} - valid settings object. | |
| 148 */ | |
| 149 v8Locale.createSettingsOrDefault_ = function(settings, defaults) { | |
| 150 if (!settings || typeof(settings) !== 'object' ) { | |
| 151 return defaults; | |
| 152 } | |
| 153 for (var key in defaults) { | |
| 154 if (!settings.hasOwnProperty(key)) { | |
| 155 settings[key] = defaults[key]; | |
| 156 } | |
| 157 } | |
| 158 // Clean up values, like trimming whitespace. | |
| 159 for (var key in settings) { | |
| 160 if (typeof(settings[key]) === "string") { | |
| 161 settings[key] = settings[key].trim(); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 return settings; | |
| 166 }; | |
| 167 | |
| 168 /** | |
| 169 * If locale is valid (defined and of v8Locale type) we return it. If not | |
| 170 * we create default locale and return it. | |
| 171 * @param {!Object} locale - user provided locale. | |
| 172 * @returns {Object} - v8Locale object. | |
| 173 */ | |
| 174 v8Locale.createLocaleOrDefault_ = function(locale) { | |
| 175 if (!locale || !(locale instanceof v8Locale)) { | |
| 176 return new v8Locale(); | |
| 177 } else { | |
| 178 return locale; | |
| 179 } | |
| 180 }; | |
| OLD | NEW |