| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 248 |
| 249 return resolved; | 249 return resolved; |
| 250 } | 250 } |
| 251 | 251 |
| 252 | 252 |
| 253 /** | 253 /** |
| 254 * Returns best matched supported locale and extension info using basic | 254 * Returns best matched supported locale and extension info using basic |
| 255 * lookup algorithm. | 255 * lookup algorithm. |
| 256 */ | 256 */ |
| 257 function lookupMatcher(service, requestedLocales) { | 257 function lookupMatcher(service, requestedLocales) { |
| 258 native function NativeJSGetDefaultICULocale(); | |
| 259 | |
| 260 if (service.match(SERVICE_RE) === null) { | 258 if (service.match(SERVICE_RE) === null) { |
| 261 throw new Error('Internal error, wrong service type: ' + service); | 259 throw new Error('Internal error, wrong service type: ' + service); |
| 262 } | 260 } |
| 263 | 261 |
| 264 // Cache these, they don't ever change per service. | 262 // Cache these, they don't ever change per service. |
| 265 if (AVAILABLE_LOCALES[service] === undefined) { | 263 if (AVAILABLE_LOCALES[service] === undefined) { |
| 266 AVAILABLE_LOCALES[service] = getAvailableLocalesOf(service); | 264 AVAILABLE_LOCALES[service] = getAvailableLocalesOf(service); |
| 267 } | 265 } |
| 268 | 266 |
| 269 for (var i = 0; i < requestedLocales.length; ++i) { | 267 for (var i = 0; i < requestedLocales.length; ++i) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 280 var pos = locale.lastIndexOf('-'); | 278 var pos = locale.lastIndexOf('-'); |
| 281 if (pos === -1) { | 279 if (pos === -1) { |
| 282 break; | 280 break; |
| 283 } | 281 } |
| 284 locale = locale.substring(0, pos); | 282 locale = locale.substring(0, pos); |
| 285 } while (true); | 283 } while (true); |
| 286 } | 284 } |
| 287 | 285 |
| 288 // Didn't find a match, return default. | 286 // Didn't find a match, return default. |
| 289 if (DEFAULT_ICU_LOCALE === undefined) { | 287 if (DEFAULT_ICU_LOCALE === undefined) { |
| 290 DEFAULT_ICU_LOCALE = NativeJSGetDefaultICULocale(); | 288 DEFAULT_ICU_LOCALE = %GetDefaultICULocale(); |
| 291 } | 289 } |
| 292 | 290 |
| 293 return {'locale': DEFAULT_ICU_LOCALE, 'extension': '', 'position': -1}; | 291 return {'locale': DEFAULT_ICU_LOCALE, 'extension': '', 'position': -1}; |
| 294 } | 292 } |
| 295 | 293 |
| 296 | 294 |
| 297 /** | 295 /** |
| 298 * Returns best matched supported locale and extension info using | 296 * Returns best matched supported locale and extension info using |
| 299 * implementation dependend algorithm. | 297 * implementation dependend algorithm. |
| 300 */ | 298 */ |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 * It's sometimes desireable to leave user requested locale instead of ICU | 437 * It's sometimes desireable to leave user requested locale instead of ICU |
| 440 * supported one (zh-TW is equivalent to zh-Hant-TW, so we should keep shorter | 438 * supported one (zh-TW is equivalent to zh-Hant-TW, so we should keep shorter |
| 441 * one, if that was what user requested). | 439 * one, if that was what user requested). |
| 442 * This function returns user specified tag if its maximized form matches ICU | 440 * This function returns user specified tag if its maximized form matches ICU |
| 443 * resolved locale. If not we return ICU result. | 441 * resolved locale. If not we return ICU result. |
| 444 */ | 442 */ |
| 445 function getOptimalLanguageTag(original, resolved) { | 443 function getOptimalLanguageTag(original, resolved) { |
| 446 // Returns Array<Object>, where each object has maximized and base properties. | 444 // Returns Array<Object>, where each object has maximized and base properties. |
| 447 // Maximized: zh -> zh-Hans-CN | 445 // Maximized: zh -> zh-Hans-CN |
| 448 // Base: zh-CN-u-ca-gregory -> zh-CN | 446 // Base: zh-CN-u-ca-gregory -> zh-CN |
| 449 native function NativeJSGetLanguageTagVariants(); | |
| 450 | |
| 451 // Take care of grandfathered or simple cases. | 447 // Take care of grandfathered or simple cases. |
| 452 if (original === resolved) { | 448 if (original === resolved) { |
| 453 return original; | 449 return original; |
| 454 } | 450 } |
| 455 | 451 |
| 456 var locales = NativeJSGetLanguageTagVariants([original, resolved]); | 452 var locales = %GetLanguageTagVariants([original, resolved]); |
| 457 if (locales[0].maximized !== locales[1].maximized) { | 453 if (locales[0].maximized !== locales[1].maximized) { |
| 458 return resolved; | 454 return resolved; |
| 459 } | 455 } |
| 460 | 456 |
| 461 // Preserve extensions of resolved locale, but swap base tags with original. | 457 // Preserve extensions of resolved locale, but swap base tags with original. |
| 462 var resolvedBase = new RegExp('^' + locales[1].base); | 458 var resolvedBase = new RegExp('^' + locales[1].base); |
| 463 return resolved.replace(resolvedBase, locales[0].base); | 459 return resolved.replace(resolvedBase, locales[0].base); |
| 464 } | 460 } |
| 465 | 461 |
| 466 | 462 |
| 467 /** | 463 /** |
| 468 * Returns an Object that contains all of supported locales for a given | 464 * Returns an Object that contains all of supported locales for a given |
| 469 * service. | 465 * service. |
| 470 * In addition to the supported locales we add xx-ZZ locale for each xx-Yyyy-ZZ | 466 * In addition to the supported locales we add xx-ZZ locale for each xx-Yyyy-ZZ |
| 471 * that is supported. This is required by the spec. | 467 * that is supported. This is required by the spec. |
| 472 */ | 468 */ |
| 473 function getAvailableLocalesOf(service) { | 469 function getAvailableLocalesOf(service) { |
| 474 native function NativeJSAvailableLocalesOf(); | 470 var available = %AvailableLocalesOf(service); |
| 475 var available = NativeJSAvailableLocalesOf(service); | |
| 476 | 471 |
| 477 for (var i in available) { | 472 for (var i in available) { |
| 478 if (available.hasOwnProperty(i)) { | 473 if (available.hasOwnProperty(i)) { |
| 479 var parts = i.match(/^([a-z]{2,3})-([A-Z][a-z]{3})-([A-Z]{2})$/); | 474 var parts = i.match(/^([a-z]{2,3})-([A-Z][a-z]{3})-([A-Z]{2})$/); |
| 480 if (parts !== null) { | 475 if (parts !== null) { |
| 481 // Build xx-ZZ. We don't care about the actual value, | 476 // Build xx-ZZ. We don't care about the actual value, |
| 482 // as long it's not undefined. | 477 // as long it's not undefined. |
| 483 available[parts[1] + '-' + parts[3]] = null; | 478 available[parts[1] + '-' + parts[3]] = null; |
| 484 } | 479 } |
| 485 } | 480 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 } | 527 } |
| 533 } | 528 } |
| 534 | 529 |
| 535 | 530 |
| 536 /** | 531 /** |
| 537 * Returns titlecased word, aMeRricA -> America. | 532 * Returns titlecased word, aMeRricA -> America. |
| 538 */ | 533 */ |
| 539 function toTitleCaseWord(word) { | 534 function toTitleCaseWord(word) { |
| 540 return word.substr(0, 1).toUpperCase() + word.substr(1).toLowerCase(); | 535 return word.substr(0, 1).toUpperCase() + word.substr(1).toLowerCase(); |
| 541 } | 536 } |
| OLD | NEW |