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

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

Issue 7014019: Adding DateTimeFormat class to i18n API. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: ascii->utf8. Added narrow and standalone. Added short, medium, long and full to types. 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
(...skipping 27 matching lines...) Expand all
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 * @constructor 77 * @constructor
78 */ 78 */
79 v8Locale.v8BreakIterator = function(locale, type) { 79 v8Locale.v8BreakIterator = function(locale, type) {
80 native function NativeJSBreakIterator(); 80 native function NativeJSBreakIterator();
81 81
82 locale = v8Locale.createLocaleOrDefault_(locale); 82 locale = v8Locale.__createLocaleOrDefault(locale);
83 // BCP47 ID would work in this case, but we use ICU locale for consistency. 83 // BCP47 ID would work in this case, but we use ICU locale for consistency.
84 var iterator = NativeJSBreakIterator(locale.__icuLocaleID__, type); 84 var iterator = NativeJSBreakIterator(locale.__icuLocaleID, type);
85 iterator.type = type; 85 iterator.type = type;
86 return iterator; 86 return iterator;
87 }; 87 };
88 88
89 /** 89 /**
90 * Type of the break we encountered during previous iteration. 90 * Type of the break we encountered during previous iteration.
91 * @type{Enum} 91 * @type{Enum}
92 */ 92 */
93 v8Locale.v8BreakIterator.BreakType = { 93 v8Locale.v8BreakIterator.BreakType = {
94 'unknown': -1, 94 'unknown': -1,
(...skipping 20 matching lines...) Expand all
115 * @param {Object} locale - locale object to pass to collator implementation. 115 * @param {Object} locale - locale object to pass to collator implementation.
116 * @param {Object} settings - collation flags: 116 * @param {Object} settings - collation flags:
117 * - ignoreCase 117 * - ignoreCase
118 * - ignoreAccents 118 * - ignoreAccents
119 * - numeric 119 * - numeric
120 * @constructor 120 * @constructor
121 */ 121 */
122 v8Locale.Collator = function(locale, settings) { 122 v8Locale.Collator = function(locale, settings) {
123 native function NativeJSCollator(); 123 native function NativeJSCollator();
124 124
125 locale = v8Locale.createLocaleOrDefault_(locale); 125 locale = v8Locale.__createLocaleOrDefault(locale);
126 var collator = NativeJSCollator( 126 var collator = NativeJSCollator(
127 locale.__icuLocaleID__, v8Locale.createSettingsOrDefault_(settings, {})); 127 locale.__icuLocaleID, v8Locale.__createSettingsOrDefault(settings, {}));
128 return collator; 128 return collator;
129 }; 129 };
130 130
131 /** 131 /**
132 * Creates new Collator based on current locale. 132 * Creates new Collator based on current locale.
133 * @param {Object} - collation flags. See constructor. 133 * @param {Object} - collation flags. See constructor.
134 * @returns {Object} - new v8BreakIterator object. 134 * @returns {Object} - new Collator object.
135 */ 135 */
136 v8Locale.prototype.createCollator = function(settings) { 136 v8Locale.prototype.createCollator = function(settings) {
137 return new v8Locale.Collator(this, settings); 137 return new v8Locale.Collator(this, settings);
138 }; 138 };
139 139
140 /** 140 /**
141 * DateTimeFormat class implements locale-aware date and time formatting.
142 * Constructor is not part of public API.
143 * @param {Object} locale - locale object to pass to formatter.
144 * @param {Object} settings - formatting flags:
145 * - skeleton
146 * - dateType
147 * - timeType
148 * - calendar
149 * @constructor
150 */
151 v8Locale.__DateTimeFormat = function(locale, settings) {
152 native function NativeJSDateTimeFormat();
153
154 settings = v8Locale.__createSettingsOrDefault(settings, {});
155
156 var cleanSettings = {};
157 if (settings.hasOwnProperty('skeleton')) {
158 cleanSettings['skeleton'] = settings['skeleton'];
159 } else {
160 cleanSettings = {};
161 if (settings.hasOwnProperty('dateType')) {
162 var dt = settings['dateType'];
163 if (!/^short|medium|long|full$/.test(dt)) dt = 'short';
164 cleanSettings['dateType'] = dt;
165 }
166
167 if (settings.hasOwnProperty('timeType')) {
168 var tt = settings['timeType'];
169 if (!/^short|medium|long|full$/.test(tt)) tt = 'short';
170 cleanSettings['timeType'] = tt;
171 }
172 }
173
174 // Default is to show short date and time.
175 if (!cleanSettings.hasOwnProperty('skeleton') &&
176 !cleanSettings.hasOwnProperty('dateType') &&
177 !cleanSettings.hasOwnProperty('timeType')) {
178 cleanSettings = {'dateType': 'short',
179 'timeType': 'short'};
180 }
181
182 locale = v8Locale.__createLocaleOrDefault(locale);
183 var formatter = NativeJSDateTimeFormat(locale.__icuLocaleID, cleanSettings);
184
185 // NativeJSDateTimeFormat creates formatter.options for us, we just need
186 // to append actual settings to it.
187 for (key in cleanSettings) {
188 formatter.options[key] = cleanSettings[key];
189 }
190
191 /**
192 * Clones existing date time format with possible overrides for some
193 * of the options.
194 * @param {!Object} overrideSettings - overrides for current format settings.
195 * @returns {Object} - new DateTimeFormat object.
196 */
197 formatter.derive = function(overrideSettings) {
198 // To remove a setting user can specify undefined as its value. We'll remove
199 // it from the map in that case.
200 for (var prop in overrideSettings) {
201 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) {
202 delete settings[prop];
203 }
204 }
205 return new v8Locale.__DateTimeFormat(
206 locale, v8Locale.__createSettingsOrDefault(overrideSettings, settings));
207 };
208
209 return formatter;
210 };
211
212 /**
213 * Creates new DateTimeFormat based on current locale.
214 * @param {Object} - formatting flags. See constructor.
215 * @returns {Object} - new DateTimeFormat object.
216 */
217 v8Locale.prototype.createDateTimeFormat = function(settings) {
218 return new v8Locale.__DateTimeFormat(this, settings);
219 };
220
221 /**
141 * Merges user settings and defaults. 222 * Merges user settings and defaults.
142 * Settings that are not of object type are rejected. 223 * Settings that are not of object type are rejected.
143 * Actual property values are not validated, but whitespace is trimmed if they 224 * Actual property values are not validated, but whitespace is trimmed if they
144 * are strings. 225 * are strings.
145 * @param {!Object} settings - user provided settings. 226 * @param {!Object} settings - user provided settings.
146 * @param {!Object} defaults - default values for this type of settings. 227 * @param {!Object} defaults - default values for this type of settings.
147 * @returns {Object} - valid settings object. 228 * @returns {Object} - valid settings object.
148 */ 229 */
149 v8Locale.createSettingsOrDefault_ = function(settings, defaults) { 230 v8Locale.__createSettingsOrDefault = function(settings, defaults) {
150 if (!settings || typeof(settings) !== 'object' ) { 231 if (!settings || typeof(settings) !== 'object' ) {
151 return defaults; 232 return defaults;
152 } 233 }
153 for (var key in defaults) { 234 for (var key in defaults) {
154 if (!settings.hasOwnProperty(key)) { 235 if (!settings.hasOwnProperty(key)) {
155 settings[key] = defaults[key]; 236 settings[key] = defaults[key];
156 } 237 }
157 } 238 }
158 // Clean up values, like trimming whitespace. 239 // Clean up settings.
159 for (var key in settings) { 240 for (var key in settings) {
241 // Trim whitespace.
160 if (typeof(settings[key]) === "string") { 242 if (typeof(settings[key]) === "string") {
161 settings[key] = settings[key].trim(); 243 settings[key] = settings[key].trim();
162 } 244 }
245 // Remove all properties that are set to undefined/null. This allows
246 // derive method to remove a setting we don't need anymore.
247 if (!settings[key]) {
248 delete settings[key];
249 }
163 } 250 }
164 251
165 return settings; 252 return settings;
166 }; 253 };
167 254
168 /** 255 /**
169 * If locale is valid (defined and of v8Locale type) we return it. If not 256 * If locale is valid (defined and of v8Locale type) we return it. If not
170 * we create default locale and return it. 257 * we create default locale and return it.
171 * @param {!Object} locale - user provided locale. 258 * @param {!Object} locale - user provided locale.
172 * @returns {Object} - v8Locale object. 259 * @returns {Object} - v8Locale object.
173 */ 260 */
174 v8Locale.createLocaleOrDefault_ = function(locale) { 261 v8Locale.__createLocaleOrDefault = function(locale) {
175 if (!locale || !(locale instanceof v8Locale)) { 262 if (!locale || !(locale instanceof v8Locale)) {
176 return new v8Locale(); 263 return new v8Locale();
177 } else { 264 } else {
178 return locale; 265 return locale;
179 } 266 }
180 }; 267 };
OLDNEW
« no previous file with comments | « src/extensions/experimental/experimental.gyp ('k') | src/extensions/experimental/i18n-extension.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698