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

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

Issue 6901141: Change v8Locale to match proposal - constructor is different and I've added (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: strcpy->snprintf Created 9 years, 7 months 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
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.
jungshik at Google 2011/05/02 19:57:23 nit: a link to the section of UTR 35 defining Unic
Nebojša Ćirić 2011/05/02 22:44:01 Done.
34 * {string} settings.regionID - ISO3166 region ID with addition of
35 * invalid, undefined and reserved region codes.
36 * @constructor
37 */
38 v8Locale = function(settings) {
30 native function NativeJSLocale(); 39 native function NativeJSLocale();
31 var properties = NativeJSLocale(optLocale); 40
32 this.locale = properties.locale; 41 // Assume user wanted to do v8Locale("sr");
33 this.language = properties.language; 42 if (typeof(settings) === "string") {
34 this.script = properties.script; 43 settings = {'localeID': settings};
35 this.region = properties.region; 44 }
45
46 var properties = NativeJSLocale(
47 v8Locale.createSettingsOrDefault_(settings, {'localeID': 'root'}));
48
49 // Keep ICU locale around so we don't have to convert later on.
jungshik at Google 2011/05/02 19:57:23 nit: Keep the resolved ICU locale ID around to avo
Nebojša Ćirić 2011/05/02 22:44:01 Done.
50 this.icuLocaleID_ = properties.icuLocaleID;
51 this.options = {'localeID': properties.localeID,
52 'regionID': properties.regionID};
36 }; 53 };
37 54
38 v8Locale.availableLocales = function() { 55 /**
39 native function NativeJSAvailableLocales(); 56 * Clones existing locale with possible overrides for some of the options.
40 return NativeJSAvailableLocales(); 57 * @param {!Object} settings - overrides for current locale settings.
58 * @returns {Object} - new LocaleInfo object.
59 */
60 v8Locale.prototype.derive = function(settings) {
61 return new v8Locale(
62 v8Locale.createSettingsOrDefault_(settings, this.options));
41 }; 63 };
42 64
43 v8Locale.prototype.maximizedLocale = function() { 65 /**
44 native function NativeJSMaximizedLocale(); 66 * v8BreakIterator class implements locale aware segmenatation.
45 return new v8Locale(NativeJSMaximizedLocale(this.locale)); 67 * It is not part of EcmaScript proposal.
46 }; 68 * @param {Object} locale - locale object to pass to break
47 69 * iterator implementation.
48 v8Locale.prototype.minimizedLocale = function() { 70 * @param {string} type - type of segmenatation:
49 native function NativeJSMinimizedLocale(); 71 * - character
50 return new v8Locale(NativeJSMinimizedLocale(this.locale)); 72 * - word
51 }; 73 * - sentence
52 74 * - line
53 v8Locale.prototype.displayLocale_ = function(displayLocale) { 75 * @constructor
54 var result = this.locale; 76 */
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) { 77 v8Locale.v8BreakIterator = function(locale, type) {
86 native function NativeJSBreakIterator(); 78 native function NativeJSBreakIterator();
87 var iterator = NativeJSBreakIterator(locale, type); 79
80 locale = v8Locale.createLocaleOrDefault_(locale);
81 // This should be locale.options.localeID, but ICU would require conversion.
jungshik at Google 2011/05/02 19:57:23 In this particular case (BreakIterator), just pass
Nebojša Ćirić 2011/05/02 22:44:01 Done.
82 var iterator = NativeJSBreakIterator(locale.icuLocaleID_, type);
88 iterator.type = type; 83 iterator.type = type;
89 return iterator; 84 return iterator;
90 }; 85 };
91 86
87 /**
88 * Type of the break we encountered during previous iteration.
89 * @type{Enum}
90 */
92 v8Locale.v8BreakIterator.BreakType = { 91 v8Locale.v8BreakIterator.BreakType = {
93 'unknown': -1, 92 'unknown': -1,
94 'none': 0, 93 'none': 0,
95 'number': 100, 94 'number': 100,
96 'word': 200, 95 'word': 200,
97 'kana': 300, 96 'kana': 300,
98 'ideo': 400 97 'ideo': 400
99 }; 98 };
100 99
100 /**
101 * Creates new v8BreakIterator based on current locale.
102 * @param {string} - type of segmentation. See constructor.
103 * @returns {Object} - new v8BreakIterator object.
104 */
101 v8Locale.prototype.v8CreateBreakIterator = function(type) { 105 v8Locale.prototype.v8CreateBreakIterator = function(type) {
102 return new v8Locale.v8BreakIterator(this.locale, type); 106 return new v8Locale.v8BreakIterator(this, type);
103 }; 107 };
104 108
105 // TODO(jungshik): Set |collator.options| to actually recognized / resolved 109 // TODO(jungshik): Set |collator.options| to actually recognized / resolved
106 // values. 110 // values.
107 v8Locale.Collator = function(locale, options) { 111 /**
112 * Collator class implements locale aware sort.
jungshik at Google 2011/05/02 19:57:23 nit: locale-aware or locale-sensitive
Nebojša Ćirić 2011/05/02 22:44:01 Done.
113 * @param {Object} locale - locale object to pass to collator implementation.
114 * @param {Object} settings - collation flags:
115 * - ignoreCase
116 * - ignoreAccents
117 * - numeric
118 * @constructor
119 */
120 v8Locale.Collator = function(locale, settings) {
108 native function NativeJSCollator(); 121 native function NativeJSCollator();
109 var collator = NativeJSCollator(locale, 122
110 options === undefined ? {} : options); 123 locale = v8Locale.createLocaleOrDefault_(locale);
124 var collator = NativeJSCollator(
125 locale.icuLocaleID_, v8Locale.createSettingsOrDefault_(settings, {}));
111 return collator; 126 return collator;
112 }; 127 };
113 128
114 v8Locale.prototype.createCollator = function(options) { 129 /**
115 return new v8Locale.Collator(this.locale, options); 130 * Creates new Collator based on current locale.
131 * @param {Object} - collation flags. See constructor.
132 * @returns {Object} - new v8BreakIterator object.
133 */
134 v8Locale.prototype.createCollator = function(settings) {
135 return new v8Locale.Collator(this, settings);
116 }; 136 };
137
138 /**
139 * Takes user settings and if they are valid returns them. If they are not
140 * valid takes default values and constructs new settings object.
jungshik at Google 2011/05/02 19:57:23 nit: valid takes ==> valid, takes. BTW, the descr
Nebojša Ćirić 2011/05/02 22:44:01 Done.
141 * Settings are valid if they are Object type and have all required keys -
142 * they can have more keys. Actual property values are not checked.
143 * @param {!Object} settings - user provided settings.
144 * @param {!Object} defaults - default values for this type of settings.
145 * @returns {Object} - valid settings object.
146 */
147 v8Locale.createSettingsOrDefault_ = function(settings, defaults) {
148 if (!settings || typeof(settings) !== 'object' ) {
149 return defaults;
150 }
151 for (var key in defaults) {
152 if (!settings.hasOwnProperty(key)) {
153 settings[key] = defaults[key];
154 }
155 }
156 // Clean up values, like trimming whitespace.
157 for (var key in settings) {
158 if (typeof(settings[key]) === "string") {
159 settings[key] = settings[key].trim();
160 }
161 }
162
163 return settings;
164 };
165
166 /**
167 * If locale is valid (defined and of v8Locale type) we return it. If not
168 * we create default locale and return it.
169 * @param {!Object} locale - user provided locale.
170 * @returns {Object} - v8Locale object.
171 */
172 v8Locale.createLocaleOrDefault_ = function(locale) {
173 if (!locale || !(locale instanceof v8Locale)) {
174 return new v8Locale();
175 } else {
176 return locale;
177 }
178 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698