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): Remove v8 prefix from v8Locale once we have stable API. | 28 // TODO(cira): Rename v8Locale into LocaleInfo once we have stable API. |
29 v8Locale = function(optLocale) { | 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) { |
30 native function NativeJSLocale(); | 40 native function NativeJSLocale(); |
31 var properties = NativeJSLocale(optLocale); | 41 |
32 this.locale = properties.locale; | 42 // Assume user wanted to do v8Locale("sr"); |
33 this.language = properties.language; | 43 if (typeof(settings) === "string") { |
34 this.script = properties.script; | 44 settings = {'localeID': settings}; |
35 this.region = properties.region; | 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}; |
36 }; | 55 }; |
37 | 56 |
38 v8Locale.availableLocales = function() { | 57 /** |
39 native function NativeJSAvailableLocales(); | 58 * Clones existing locale with possible overrides for some of the options. |
40 return NativeJSAvailableLocales(); | 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)); |
41 }; | 65 }; |
42 | 66 |
43 v8Locale.prototype.maximizedLocale = function() { | 67 /** |
44 native function NativeJSMaximizedLocale(); | 68 * v8BreakIterator class implements locale aware segmenatation. |
45 return new v8Locale(NativeJSMaximizedLocale(this.locale)); | 69 * It is not part of EcmaScript proposal. |
46 }; | 70 * @param {Object} locale - locale object to pass to break |
47 | 71 * iterator implementation. |
48 v8Locale.prototype.minimizedLocale = function() { | 72 * @param {string} type - type of segmenatation: |
49 native function NativeJSMinimizedLocale(); | 73 * - character |
50 return new v8Locale(NativeJSMinimizedLocale(this.locale)); | 74 * - word |
51 }; | 75 * - sentence |
52 | 76 * - line |
53 v8Locale.prototype.displayLocale_ = function(displayLocale) { | 77 * @constructor |
54 var result = this.locale; | 78 */ |
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 | |
85 v8Locale.v8BreakIterator = function(locale, type) { | 79 v8Locale.v8BreakIterator = function(locale, type) { |
86 native function NativeJSBreakIterator(); | 80 native function NativeJSBreakIterator(); |
87 var iterator = NativeJSBreakIterator(locale, type); | 81 |
| 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); |
88 iterator.type = type; | 85 iterator.type = type; |
89 return iterator; | 86 return iterator; |
90 }; | 87 }; |
91 | 88 |
| 89 /** |
| 90 * Type of the break we encountered during previous iteration. |
| 91 * @type{Enum} |
| 92 */ |
92 v8Locale.v8BreakIterator.BreakType = { | 93 v8Locale.v8BreakIterator.BreakType = { |
93 'unknown': -1, | 94 'unknown': -1, |
94 'none': 0, | 95 'none': 0, |
95 'number': 100, | 96 'number': 100, |
96 'word': 200, | 97 'word': 200, |
97 'kana': 300, | 98 'kana': 300, |
98 'ideo': 400 | 99 'ideo': 400 |
99 }; | 100 }; |
100 | 101 |
| 102 /** |
| 103 * Creates new v8BreakIterator based on current locale. |
| 104 * @param {string} - type of segmentation. See constructor. |
| 105 * @returns {Object} - new v8BreakIterator object. |
| 106 */ |
101 v8Locale.prototype.v8CreateBreakIterator = function(type) { | 107 v8Locale.prototype.v8CreateBreakIterator = function(type) { |
102 return new v8Locale.v8BreakIterator(this.locale, type); | 108 return new v8Locale.v8BreakIterator(this, type); |
103 }; | 109 }; |
104 | 110 |
105 // TODO(jungshik): Set |collator.options| to actually recognized / resolved | 111 // TODO(jungshik): Set |collator.options| to actually recognized / resolved |
106 // values. | 112 // values. |
107 v8Locale.Collator = function(locale, options) { | 113 /** |
| 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) { |
108 native function NativeJSCollator(); | 123 native function NativeJSCollator(); |
109 var collator = NativeJSCollator(locale, | 124 |
110 options === undefined ? {} : options); | 125 locale = v8Locale.createLocaleOrDefault_(locale); |
| 126 var collator = NativeJSCollator( |
| 127 locale.__icuLocaleID__, v8Locale.createSettingsOrDefault_(settings, {})); |
111 return collator; | 128 return collator; |
112 }; | 129 }; |
113 | 130 |
114 v8Locale.prototype.createCollator = function(options) { | 131 /** |
115 return new v8Locale.Collator(this.locale, options); | 132 * Creates new Collator based on current locale. |
| 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); |
116 }; | 138 }; |
| 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 |