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 |
(...skipping 27 matching lines...) Expand all Loading... |
38 */ | 38 */ |
39 v8Locale = function(settings) { | 39 v8Locale = function(settings) { |
40 native function NativeJSLocale(); | 40 native function NativeJSLocale(); |
41 | 41 |
42 // Assume user wanted to do v8Locale("sr"); | 42 // Assume user wanted to do v8Locale("sr"); |
43 if (typeof(settings) === "string") { | 43 if (typeof(settings) === "string") { |
44 settings = {'localeID': settings}; | 44 settings = {'localeID': settings}; |
45 } | 45 } |
46 | 46 |
47 var properties = NativeJSLocale( | 47 var properties = NativeJSLocale( |
48 v8Locale.createSettingsOrDefault_(settings, {'localeID': 'root'})); | 48 v8Locale.__createSettingsOrDefault(settings, {'localeID': 'root'})); |
49 | 49 |
50 // Keep the resolved ICU locale ID around to avoid resolving localeID to | 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. | 51 // ICU locale ID every time BreakIterator, Collator and so forth are called. |
52 this.__icuLocaleID__ = properties.icuLocaleID; | 52 this.__icuLocaleID = properties.icuLocaleID; |
53 this.options = {'localeID': properties.localeID, | 53 this.options = {'localeID': properties.localeID, |
54 'regionID': properties.regionID}; | 54 'regionID': properties.regionID}; |
55 }; | 55 }; |
56 | 56 |
57 /** | 57 /** |
58 * Clones existing locale with possible overrides for some of the options. | 58 * Clones existing locale with possible overrides for some of the options. |
59 * @param {!Object} settings - overrides for current locale settings. | 59 * @param {!Object} settings - overrides for current locale settings. |
60 * @returns {Object} - new LocaleInfo object. | 60 * @returns {Object} - new LocaleInfo object. |
61 */ | 61 */ |
62 v8Locale.prototype.derive = function(settings) { | 62 v8Locale.prototype.derive = function(settings) { |
63 return new v8Locale( | 63 return new v8Locale( |
64 v8Locale.createSettingsOrDefault_(settings, this.options)); | 64 v8Locale.__createSettingsOrDefault(settings, this.options)); |
65 }; | 65 }; |
66 | 66 |
67 /** | 67 /** |
68 * v8BreakIterator class implements locale aware segmenatation. | 68 * v8BreakIterator class implements locale aware segmenatation. |
69 * It is not part of EcmaScript proposal. | 69 * It is not part of EcmaScript proposal. |
70 * @param {Object} locale - locale object to pass to break | 70 * @param {Object} locale - locale object to pass to break |
71 * iterator implementation. | 71 * iterator implementation. |
72 * @param {string} type - type of segmenatation: | 72 * @param {string} type - type of segmenatation: |
73 * - character | 73 * - character |
74 * - word | 74 * - word |
75 * - sentence | 75 * - sentence |
76 * - line | 76 * - line |
| 77 * @private |
77 * @constructor | 78 * @constructor |
78 */ | 79 */ |
79 v8Locale.v8BreakIterator = function(locale, type) { | 80 v8Locale.v8BreakIterator = function(locale, type) { |
80 native function NativeJSBreakIterator(); | 81 native function NativeJSBreakIterator(); |
81 | 82 |
82 locale = v8Locale.createLocaleOrDefault_(locale); | 83 locale = v8Locale.__createLocaleOrDefault(locale); |
83 // BCP47 ID would work in this case, but we use ICU locale for consistency. | 84 // BCP47 ID would work in this case, but we use ICU locale for consistency. |
84 var iterator = NativeJSBreakIterator(locale.__icuLocaleID__, type); | 85 var iterator = NativeJSBreakIterator(locale.__icuLocaleID, type); |
85 iterator.type = type; | 86 iterator.type = type; |
86 return iterator; | 87 return iterator; |
87 }; | 88 }; |
88 | 89 |
89 /** | 90 /** |
90 * Type of the break we encountered during previous iteration. | 91 * Type of the break we encountered during previous iteration. |
91 * @type{Enum} | 92 * @type{Enum} |
92 */ | 93 */ |
93 v8Locale.v8BreakIterator.BreakType = { | 94 v8Locale.v8BreakIterator.BreakType = { |
94 'unknown': -1, | 95 'unknown': -1, |
(...skipping 15 matching lines...) Expand all Loading... |
110 | 111 |
111 // TODO(jungshik): Set |collator.options| to actually recognized / resolved | 112 // TODO(jungshik): Set |collator.options| to actually recognized / resolved |
112 // values. | 113 // values. |
113 /** | 114 /** |
114 * Collator class implements locale-aware sort. | 115 * Collator class implements locale-aware sort. |
115 * @param {Object} locale - locale object to pass to collator implementation. | 116 * @param {Object} locale - locale object to pass to collator implementation. |
116 * @param {Object} settings - collation flags: | 117 * @param {Object} settings - collation flags: |
117 * - ignoreCase | 118 * - ignoreCase |
118 * - ignoreAccents | 119 * - ignoreAccents |
119 * - numeric | 120 * - numeric |
| 121 * @private |
120 * @constructor | 122 * @constructor |
121 */ | 123 */ |
122 v8Locale.Collator = function(locale, settings) { | 124 v8Locale.Collator = function(locale, settings) { |
123 native function NativeJSCollator(); | 125 native function NativeJSCollator(); |
124 | 126 |
125 locale = v8Locale.createLocaleOrDefault_(locale); | 127 locale = v8Locale.__createLocaleOrDefault(locale); |
126 var collator = NativeJSCollator( | 128 var collator = NativeJSCollator( |
127 locale.__icuLocaleID__, v8Locale.createSettingsOrDefault_(settings, {})); | 129 locale.__icuLocaleID, v8Locale.__createSettingsOrDefault(settings, {})); |
128 return collator; | 130 return collator; |
129 }; | 131 }; |
130 | 132 |
131 /** | 133 /** |
132 * Creates new Collator based on current locale. | 134 * Creates new Collator based on current locale. |
133 * @param {Object} - collation flags. See constructor. | 135 * @param {Object} - collation flags. See constructor. |
134 * @returns {Object} - new v8BreakIterator object. | 136 * @returns {Object} - new Collator object. |
135 */ | 137 */ |
136 v8Locale.prototype.createCollator = function(settings) { | 138 v8Locale.prototype.createCollator = function(settings) { |
137 return new v8Locale.Collator(this, settings); | 139 return new v8Locale.Collator(this, settings); |
138 }; | 140 }; |
139 | 141 |
140 /** | 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 /** |
141 * Merges user settings and defaults. | 226 * Merges user settings and defaults. |
142 * Settings that are not of object type are rejected. | 227 * Settings that are not of object type are rejected. |
143 * Actual property values are not validated, but whitespace is trimmed if they | 228 * Actual property values are not validated, but whitespace is trimmed if they |
144 * are strings. | 229 * are strings. |
145 * @param {!Object} settings - user provided settings. | 230 * @param {!Object} settings - user provided settings. |
146 * @param {!Object} defaults - default values for this type of settings. | 231 * @param {!Object} defaults - default values for this type of settings. |
147 * @returns {Object} - valid settings object. | 232 * @returns {Object} - valid settings object. |
| 233 * @private |
148 */ | 234 */ |
149 v8Locale.createSettingsOrDefault_ = function(settings, defaults) { | 235 v8Locale.__createSettingsOrDefault = function(settings, defaults) { |
150 if (!settings || typeof(settings) !== 'object' ) { | 236 if (!settings || typeof(settings) !== 'object' ) { |
151 return defaults; | 237 return defaults; |
152 } | 238 } |
153 for (var key in defaults) { | 239 for (var key in defaults) { |
154 if (!settings.hasOwnProperty(key)) { | 240 if (!settings.hasOwnProperty(key)) { |
155 settings[key] = defaults[key]; | 241 settings[key] = defaults[key]; |
156 } | 242 } |
157 } | 243 } |
158 // Clean up values, like trimming whitespace. | 244 // Clean up settings. |
159 for (var key in settings) { | 245 for (var key in settings) { |
| 246 // Trim whitespace. |
160 if (typeof(settings[key]) === "string") { | 247 if (typeof(settings[key]) === "string") { |
161 settings[key] = settings[key].trim(); | 248 settings[key] = settings[key].trim(); |
162 } | 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 } |
163 } | 255 } |
164 | 256 |
165 return settings; | 257 return settings; |
166 }; | 258 }; |
167 | 259 |
168 /** | 260 /** |
169 * If locale is valid (defined and of v8Locale type) we return it. If not | 261 * If locale is valid (defined and of v8Locale type) we return it. If not |
170 * we create default locale and return it. | 262 * we create default locale and return it. |
171 * @param {!Object} locale - user provided locale. | 263 * @param {!Object} locale - user provided locale. |
172 * @returns {Object} - v8Locale object. | 264 * @returns {Object} - v8Locale object. |
| 265 * @private |
173 */ | 266 */ |
174 v8Locale.createLocaleOrDefault_ = function(locale) { | 267 v8Locale.__createLocaleOrDefault = function(locale) { |
175 if (!locale || !(locale instanceof v8Locale)) { | 268 if (!locale || !(locale instanceof v8Locale)) { |
176 return new v8Locale(); | 269 return new v8Locale(); |
177 } else { | 270 } else { |
178 return locale; | 271 return locale; |
179 } | 272 } |
180 }; | 273 }; |
OLD | NEW |