Chromium Code Reviews| 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 shortDate = 'yMMdd'; | |
|
jungshik at Google
2011/05/13 23:37:53
Hmm... this is problematic in some locales. In Kor
Nebojša Ćirić
2011/05/17 20:40:37
Done.
| |
| 156 var longDate = 'EEEEyMMMdd'; | |
| 157 var shortTime = 'jm'; | |
| 158 var mediumTime = 'jms'; | |
| 159 var skeleton = shortDate + shortTime; | |
| 160 if (settings.hasOwnProperty('skeleton')) { | |
| 161 skeleton = settings['skeleton']; | |
| 162 } else if (settings.hasOwnProperty('dateType') && | |
| 163 settings.hasOwnProperty('timeType')) { | |
|
jungshik at Google
2011/05/13 23:37:53
After our discussion, this point became unapplicab
Nebojša Ćirić
2011/05/17 20:40:37
Done.
| |
| 164 var dt = settings['dateType']; | |
| 165 var tt = settings['timeType']; | |
| 166 if (dt === 'short' && tt === 'short') { | |
| 167 skeleton = shortDate + shortTime; | |
| 168 } else if (dt === 'short' && tt === 'medium') { | |
| 169 skeleton = shortDate + mediumTime; | |
| 170 } else if (dt === 'long' && tt === 'short') { | |
| 171 skeleton = longDate + shortTime; | |
| 172 } else if (dt === 'long' && tt === 'medium') { | |
| 173 skeleton = longDate + mediumTime; | |
| 174 } | |
| 175 } else if (settings.hasOwnProperty('dateType')) { | |
| 176 if (settings['dateType'] === 'short') { | |
| 177 skeleton = shortDate; | |
| 178 } else if (settings['dateType'] === 'long') { | |
| 179 skeleton = longDate; | |
| 180 } | |
| 181 } else if (settings.hasOwnProperty('timeType')) { | |
| 182 if (settings['timeType'] === 'short') { | |
| 183 skeleton = shortTime; | |
| 184 } else if (settings['timeType'] === 'medium') { | |
| 185 skeleton = mediumTime; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 locale = v8Locale.createLocaleOrDefault_(locale); | |
| 190 var formatter = NativeJSDateTimeFormat(locale.__icuLocaleID__, skeleton); | |
| 191 | |
| 192 /** | |
| 193 * Clones existing date time format with possible overrides for some | |
| 194 * of the options. | |
| 195 * @param {!Object} overrideSettings - overrides for current format settings. | |
| 196 * @returns {Object} - new DateTimeFormat object. | |
| 197 */ | |
| 198 formatter.derive = function(overrideSettings) { | |
| 199 return new v8Locale.DateTimeFormat( | |
| 200 locale, v8Locale.createSettingsOrDefault_(overrideSettings, settings)); | |
| 201 }; | |
| 202 | |
| 203 return formatter; | |
| 204 }; | |
| 205 | |
| 206 /** | |
| 207 * Creates new DateTimeFormat based on current locale. | |
| 208 * @param {Object} - formatting flags. See constructor. | |
| 209 * @returns {Object} - new DateTimeFormat object. | |
| 210 */ | |
| 211 v8Locale.prototype.createDateTimeFormat = function(settings) { | |
| 212 return new v8Locale.DateTimeFormat(this, settings); | |
| 213 }; | |
| 214 | |
| 215 /** | |
| 141 * Merges user settings and defaults. | 216 * Merges user settings and defaults. |
| 142 * Settings that are not of object type are rejected. | 217 * Settings that are not of object type are rejected. |
| 143 * Actual property values are not validated, but whitespace is trimmed if they | 218 * Actual property values are not validated, but whitespace is trimmed if they |
| 144 * are strings. | 219 * are strings. |
| 145 * @param {!Object} settings - user provided settings. | 220 * @param {!Object} settings - user provided settings. |
| 146 * @param {!Object} defaults - default values for this type of settings. | 221 * @param {!Object} defaults - default values for this type of settings. |
| 147 * @returns {Object} - valid settings object. | 222 * @returns {Object} - valid settings object. |
| 148 */ | 223 */ |
| 149 v8Locale.createSettingsOrDefault_ = function(settings, defaults) { | 224 v8Locale.createSettingsOrDefault_ = function(settings, defaults) { |
| 150 if (!settings || typeof(settings) !== 'object' ) { | 225 if (!settings || typeof(settings) !== 'object' ) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 171 * @param {!Object} locale - user provided locale. | 246 * @param {!Object} locale - user provided locale. |
| 172 * @returns {Object} - v8Locale object. | 247 * @returns {Object} - v8Locale object. |
| 173 */ | 248 */ |
| 174 v8Locale.createLocaleOrDefault_ = function(locale) { | 249 v8Locale.createLocaleOrDefault_ = function(locale) { |
| 175 if (!locale || !(locale instanceof v8Locale)) { | 250 if (!locale || !(locale instanceof v8Locale)) { |
| 176 return new v8Locale(); | 251 return new v8Locale(); |
| 177 } else { | 252 } else { |
| 178 return locale; | 253 return locale; |
| 179 } | 254 } |
| 180 }; | 255 }; |
| OLD | NEW |