Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: src/extensions/experimental/i18n.js

Issue 9016034: Removing experimental i18n code from v8 repository. Internationalization support is hosted under ... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 * - dateStyle
149 * - timeStyle
150 * @private
151 * @constructor
152 */
153 v8Locale.__DateTimeFormat = function(locale, settings) {
154 native function NativeJSDateTimeFormat();
155
156 settings = v8Locale.__createSettingsOrDefault(settings, {});
157
158 var cleanSettings = {};
159 if (settings.hasOwnProperty('skeleton')) {
160 cleanSettings['skeleton'] = settings['skeleton'];
161 } else {
162 cleanSettings = {};
163 if (settings.hasOwnProperty('dateStyle')) {
164 var ds = settings['dateStyle'];
165 if (!/^(short|medium|long|full)$/.test(ds)) ds = 'short';
166 cleanSettings['dateStyle'] = ds;
167 } else if (settings.hasOwnProperty('dateType')) {
168 // Obsolete. New spec requires dateStyle, but we'll keep this around
169 // for current users.
170 // TODO(cira): Remove when all internal users switch to dateStyle.
171 var dt = settings['dateType'];
172 if (!/^(short|medium|long|full)$/.test(dt)) dt = 'short';
173 cleanSettings['dateStyle'] = dt;
174 }
175
176 if (settings.hasOwnProperty('timeStyle')) {
177 var ts = settings['timeStyle'];
178 if (!/^(short|medium|long|full)$/.test(ts)) ts = 'short';
179 cleanSettings['timeStyle'] = ts;
180 } else if (settings.hasOwnProperty('timeType')) {
181 // TODO(cira): Remove when all internal users switch to timeStyle.
182 var tt = settings['timeType'];
183 if (!/^(short|medium|long|full)$/.test(tt)) tt = 'short';
184 cleanSettings['timeStyle'] = tt;
185 }
186 }
187
188 // Default is to show short date and time.
189 if (!cleanSettings.hasOwnProperty('skeleton') &&
190 !cleanSettings.hasOwnProperty('dateStyle') &&
191 !cleanSettings.hasOwnProperty('timeStyle')) {
192 cleanSettings = {'dateStyle': 'short',
193 'timeStyle': 'short'};
194 }
195
196 locale = v8Locale.__createLocaleOrDefault(locale);
197 var formatter = NativeJSDateTimeFormat(locale.__icuLocaleID, cleanSettings);
198
199 // NativeJSDateTimeFormat creates formatter.options for us, we just need
200 // to append actual settings to it.
201 for (key in cleanSettings) {
202 formatter.options[key] = cleanSettings[key];
203 }
204
205 /**
206 * Clones existing date time format with possible overrides for some
207 * of the options.
208 * @param {!Object} overrideSettings - overrides for current format settings.
209 * @returns {Object} - new DateTimeFormat object.
210 * @public
211 */
212 formatter.derive = function(overrideSettings) {
213 // To remove a setting user can specify undefined as its value. We'll remove
214 // it from the map in that case.
215 for (var prop in overrideSettings) {
216 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) {
217 delete settings[prop];
218 }
219 }
220 return new v8Locale.__DateTimeFormat(
221 locale, v8Locale.__createSettingsOrDefault(overrideSettings, settings));
222 };
223
224 return formatter;
225 };
226
227 /**
228 * Creates new DateTimeFormat based on current locale.
229 * @param {Object} - formatting flags. See constructor.
230 * @returns {Object} - new DateTimeFormat object.
231 */
232 v8Locale.prototype.createDateTimeFormat = function(settings) {
233 return new v8Locale.__DateTimeFormat(this, settings);
234 };
235
236 /**
237 * NumberFormat class implements locale-aware number formatting.
238 * Constructor is not part of public API.
239 * @param {Object} locale - locale object to pass to formatter.
240 * @param {Object} settings - formatting flags:
241 * - skeleton
242 * - pattern
243 * - style - decimal, currency, percent or scientific
244 * - currencyCode - ISO 4217 3-letter currency code
245 * @private
246 * @constructor
247 */
248 v8Locale.__NumberFormat = function(locale, settings) {
249 native function NativeJSNumberFormat();
250
251 settings = v8Locale.__createSettingsOrDefault(settings, {});
252
253 var cleanSettings = {};
254 if (settings.hasOwnProperty('skeleton')) {
255 // Assign skeleton to cleanSettings and fix invalid currency pattern
256 // if present - 'ooxo' becomes 'o'.
257 cleanSettings['skeleton'] =
258 settings['skeleton'].replace(/\u00a4+[^\u00a4]+\u00a4+/g, '\u00a4');
259 } else if (settings.hasOwnProperty('pattern')) {
260 cleanSettings['pattern'] = settings['pattern'];
261 } else if (settings.hasOwnProperty('style')) {
262 var style = settings['style'];
263 if (!/^(decimal|currency|percent|scientific)$/.test(style)) {
264 style = 'decimal';
265 }
266 cleanSettings['style'] = style;
267 }
268
269 // Default is to show decimal style.
270 if (!cleanSettings.hasOwnProperty('skeleton') &&
271 !cleanSettings.hasOwnProperty('pattern') &&
272 !cleanSettings.hasOwnProperty('style')) {
273 cleanSettings = {'style': 'decimal'};
274 }
275
276 // Add currency code if available and valid (3-letter ASCII code).
277 if (settings.hasOwnProperty('currencyCode') &&
278 /^[a-zA-Z]{3}$/.test(settings['currencyCode'])) {
279 cleanSettings['currencyCode'] = settings['currencyCode'].toUpperCase();
280 }
281
282 locale = v8Locale.__createLocaleOrDefault(locale);
283 // Pass in region ID for proper currency detection. Use ZZ if region is empty.
284 var region = locale.options.regionID !== '' ? locale.options.regionID : 'ZZ';
285 var formatter = NativeJSNumberFormat(
286 locale.__icuLocaleID, 'und_' + region, cleanSettings);
287
288 // ICU doesn't always uppercase the currency code.
289 if (formatter.options.hasOwnProperty('currencyCode')) {
290 formatter.options['currencyCode'] =
291 formatter.options['currencyCode'].toUpperCase();
292 }
293
294 for (key in cleanSettings) {
295 // Don't overwrite keys that are alredy in.
296 if (formatter.options.hasOwnProperty(key)) continue;
297
298 formatter.options[key] = cleanSettings[key];
299 }
300
301 /**
302 * Clones existing number format with possible overrides for some
303 * of the options.
304 * @param {!Object} overrideSettings - overrides for current format settings.
305 * @returns {Object} - new or cached NumberFormat object.
306 * @public
307 */
308 formatter.derive = function(overrideSettings) {
309 // To remove a setting user can specify undefined as its value. We'll remove
310 // it from the map in that case.
311 for (var prop in overrideSettings) {
312 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) {
313 delete settings[prop];
314 }
315 }
316 return new v8Locale.__NumberFormat(
317 locale, v8Locale.__createSettingsOrDefault(overrideSettings, settings));
318 };
319
320 return formatter;
321 };
322
323 /**
324 * Creates new NumberFormat based on current locale.
325 * @param {Object} - formatting flags. See constructor.
326 * @returns {Object} - new or cached NumberFormat object.
327 */
328 v8Locale.prototype.createNumberFormat = function(settings) {
329 return new v8Locale.__NumberFormat(this, settings);
330 };
331
332 /**
333 * Merges user settings and defaults.
334 * Settings that are not of object type are rejected.
335 * Actual property values are not validated, but whitespace is trimmed if they
336 * are strings.
337 * @param {!Object} settings - user provided settings.
338 * @param {!Object} defaults - default values for this type of settings.
339 * @returns {Object} - valid settings object.
340 * @private
341 */
342 v8Locale.__createSettingsOrDefault = function(settings, defaults) {
343 if (!settings || typeof(settings) !== 'object' ) {
344 return defaults;
345 }
346 for (var key in defaults) {
347 if (!settings.hasOwnProperty(key)) {
348 settings[key] = defaults[key];
349 }
350 }
351 // Clean up settings.
352 for (var key in settings) {
353 // Trim whitespace.
354 if (typeof(settings[key]) === "string") {
355 settings[key] = settings[key].trim();
356 }
357 // Remove all properties that are set to undefined/null. This allows
358 // derive method to remove a setting we don't need anymore.
359 if (!settings[key]) {
360 delete settings[key];
361 }
362 }
363
364 return settings;
365 };
366
367 /**
368 * If locale is valid (defined and of v8Locale type) we return it. If not
369 * we create default locale and return it.
370 * @param {!Object} locale - user provided locale.
371 * @returns {Object} - v8Locale object.
372 * @private
373 */
374 v8Locale.__createLocaleOrDefault = function(locale) {
375 if (!locale || !(locale instanceof v8Locale)) {
376 return new v8Locale();
377 } else {
378 return locale;
379 }
380 };
OLDNEW
« no previous file with comments | « src/extensions/experimental/experimental.gyp ('k') | src/extensions/experimental/i18n-extension.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698