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

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: '' 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 * @param {Object} locale - locale object to pass to formatter.
143 * @param {Object} settings - formatting flags:
144 * - skeleton
145 * - dateType
146 * - timeType
147 * - calendar
148 * @constructor
149 */
150 v8Locale.DateTimeFormat = function(locale, settings) {
151 native function NativeJSDateTimeFormat();
152
153 settings = v8Locale.createSettingsOrDefault_(settings, {});
154
155 var cleanSettings = {};
156 if (settings.hasOwnProperty('skeleton')) {
157 cleanSettings['skeleton'] = settings['skeleton'];
158 } else {
159 cleanSettings = {};
160 if (settings.hasOwnProperty('dateType')) {
161 var dt = settings['dateType'].toLowerCase();
162 if (dt !== 'short' && dt !== 'long') dt = 'short';
163 cleanSettings['dateType'] = dt;
164 }
165
166 if (settings.hasOwnProperty('timeType')) {
167 var tt = settings['timeType'].toLowerCase();
168 if (tt !== 'short' && tt !== 'medium') tt = 'short';
169 cleanSettings['timeType'] = tt;
170 }
171 }
172
173 // Default is to show short date and time.
174 if (!cleanSettings.hasOwnProperty('skeleton') &&
175 !cleanSettings.hasOwnProperty('dateType') &&
176 !cleanSettings.hasOwnProperty('timeType')) {
177 cleanSettings = {'dateType': 'short',
178 'timeType': 'short'};
179 }
180
181 locale = v8Locale.createLocaleOrDefault_(locale);
182 var formatter = NativeJSDateTimeFormat(locale.__icuLocaleID__, cleanSettings);
183
184 // NativeJSDateTimeFormat creates formatter.options for us, we just need
185 // to append actual settings to it.
186 for (key in cleanSettings) {
187 formatter.options[key] = cleanSettings[key];
188 }
189
190 /**
191 * Clones existing date time format with possible overrides for some
192 * of the options.
193 * @param {!Object} overrideSettings - overrides for current format settings.
194 * @returns {Object} - new DateTimeFormat object.
195 */
196 formatter.derive = function(overrideSettings) {
197 // To remove a setting user can specify undefined as its value. We'll remove
198 // it from the map in that case.
199 for (var prop in overrideSettings) {
200 if (settings.hasOwnProperty(prop) && !overrideSettings[prop]) {
201 delete settings[prop];
202 }
203 }
204 return new v8Locale.DateTimeFormat(
205 locale, v8Locale.createSettingsOrDefault_(overrideSettings, settings));
206 };
207
208 return formatter;
209 };
210
211 /**
212 * Creates new DateTimeFormat based on current locale.
213 * @param {Object} - formatting flags. See constructor.
214 * @returns {Object} - new DateTimeFormat object.
215 */
216 v8Locale.prototype.createDateTimeFormat = function(settings) {
217 return new v8Locale.DateTimeFormat(this, settings);
218 };
219
220 /**
141 * Merges user settings and defaults. 221 * Merges user settings and defaults.
142 * Settings that are not of object type are rejected. 222 * Settings that are not of object type are rejected.
143 * Actual property values are not validated, but whitespace is trimmed if they 223 * Actual property values are not validated, but whitespace is trimmed if they
144 * are strings. 224 * are strings.
145 * @param {!Object} settings - user provided settings. 225 * @param {!Object} settings - user provided settings.
146 * @param {!Object} defaults - default values for this type of settings. 226 * @param {!Object} defaults - default values for this type of settings.
147 * @returns {Object} - valid settings object. 227 * @returns {Object} - valid settings object.
148 */ 228 */
149 v8Locale.createSettingsOrDefault_ = function(settings, defaults) { 229 v8Locale.createSettingsOrDefault_ = function(settings, defaults) {
150 if (!settings || typeof(settings) !== 'object' ) { 230 if (!settings || typeof(settings) !== 'object' ) {
151 return defaults; 231 return defaults;
152 } 232 }
153 for (var key in defaults) { 233 for (var key in defaults) {
154 if (!settings.hasOwnProperty(key)) { 234 if (!settings.hasOwnProperty(key)) {
155 settings[key] = defaults[key]; 235 settings[key] = defaults[key];
156 } 236 }
157 } 237 }
158 // Clean up values, like trimming whitespace. 238 // Clean up settings.
159 for (var key in settings) { 239 for (var key in settings) {
240 // Trim whitespace.
160 if (typeof(settings[key]) === "string") { 241 if (typeof(settings[key]) === "string") {
161 settings[key] = settings[key].trim(); 242 settings[key] = settings[key].trim();
162 } 243 }
244 // Remove all properties that are set to undefined/null. This allows
245 // derive method to remove a setting we don't need anymore.
246 if (!settings[key]) {
247 delete settings[key];
248 }
163 } 249 }
164 250
165 return settings; 251 return settings;
166 }; 252 };
167 253
168 /** 254 /**
169 * If locale is valid (defined and of v8Locale type) we return it. If not 255 * If locale is valid (defined and of v8Locale type) we return it. If not
170 * we create default locale and return it. 256 * we create default locale and return it.
171 * @param {!Object} locale - user provided locale. 257 * @param {!Object} locale - user provided locale.
172 * @returns {Object} - v8Locale object. 258 * @returns {Object} - v8Locale object.
173 */ 259 */
174 v8Locale.createLocaleOrDefault_ = function(locale) { 260 v8Locale.createLocaleOrDefault_ = function(locale) {
175 if (!locale || !(locale instanceof v8Locale)) { 261 if (!locale || !(locale instanceof v8Locale)) {
176 return new v8Locale(); 262 return new v8Locale();
177 } else { 263 } else {
178 return locale; 264 return locale;
179 } 265 }
180 }; 266 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698