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

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

Issue 2239523002: Expose getCanonicalLocales() for Intl object (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: disable failing tests with comments Created 4 years, 4 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
« no previous file with comments | « no previous file | src/runtime/runtime-i18n.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // ECMAScript 402 API implementation. 5 // ECMAScript 402 API implementation.
6 6
7 /** 7 /**
8 * Intl object is a single object that has some named properties, 8 * Intl object is a single object that has some named properties,
9 * all of which are constructors. 9 * all of which are constructors.
10 */ 10 */
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 ((lowercasedPart !== 'es' && 711 ((lowercasedPart !== 'es' &&
712 lowercasedPart !== 'of' && lowercasedPart !== 'au') ? 712 lowercasedPart !== 'of' && lowercasedPart !== 'au') ?
713 toTitleCaseWord(part) : lowercasedPart); 713 toTitleCaseWord(part) : lowercasedPart);
714 } 714 }
715 } 715 }
716 return result; 716 return result;
717 } 717 }
718 718
719 /** 719 /**
720 * Canonicalizes the language tag, or throws in case the tag is invalid. 720 * Canonicalizes the language tag, or throws in case the tag is invalid.
721 * ECMA 402 9.2.1 steps 7.c ii ~ v.
721 */ 722 */
722 function canonicalizeLanguageTag(localeID) { 723 function canonicalizeLanguageTag(localeID) {
723 // null is typeof 'object' so we have to do extra check. 724 // null is typeof 'object' so we have to do extra check.
724 if ((!IS_STRING(localeID) && !IS_RECEIVER(localeID)) || 725 if ((!IS_STRING(localeID) && !IS_RECEIVER(localeID)) ||
725 IS_NULL(localeID)) { 726 IS_NULL(localeID)) {
726 throw %make_type_error(kLanguageID); 727 throw %make_type_error(kLanguageID);
727 } 728 }
728 729
729 // Optimize for the most common case; a language code alone in 730 // Optimize for the most common case; a language code alone in
730 // the canonical form/lowercase (e.g. "en", "fil"). 731 // the canonical form/lowercase (e.g. "en", "fil").
731 if (IS_STRING(localeID) && 732 if (IS_STRING(localeID) &&
732 !IS_NULL(InternalRegExpMatch(/^[a-z]{2,3}$/, localeID))) { 733 !IS_NULL(InternalRegExpMatch(/^[a-z]{2,3}$/, localeID))) {
733 return localeID; 734 return localeID;
734 } 735 }
735 736
736 var localeString = TO_STRING(localeID); 737 var localeString = TO_STRING(localeID);
737 738
738 if (isValidLanguageTag(localeString) === false) { 739 if (isStructuallyValidLanguageTag(localeString) === false) {
739 throw %make_range_error(kInvalidLanguageTag, localeString); 740 throw %make_range_error(kInvalidLanguageTag, localeString);
740 } 741 }
741 742
743 // ECMA 402 6.2.3
742 var tag = %CanonicalizeLanguageTag(localeString); 744 var tag = %CanonicalizeLanguageTag(localeString);
745 // TODO(jshin): This should not happen because the structual validity
746 // is already checked. If that's the case, remove this.
743 if (tag === 'invalid-tag') { 747 if (tag === 'invalid-tag') {
744 throw %make_range_error(kInvalidLanguageTag, localeString); 748 throw %make_range_error(kInvalidLanguageTag, localeString);
745 } 749 }
746 750
747 return tag; 751 return tag;
748 } 752 }
749 753
750 754
751 /** 755 /**
752 * Returns an array where all locales are canonicalized and duplicates removed. 756 * Returns an array where all locales are canonicalized and duplicates removed.
753 * Throws on locales that are not well formed BCP47 tags. 757 * Throws on locales that are not well formed BCP47 tags.
758 * ECMA 402 8.2.1 steps 1 (ECMA 402 9.2.1) and 2.
754 */ 759 */
755 function initializeLocaleList(locales) { 760 function initializeLocaleList(locales) {
756 var seen = new InternalArray(); 761 var seen = new InternalArray();
757 if (!IS_UNDEFINED(locales)) { 762 if (!IS_UNDEFINED(locales)) {
758 // We allow single string localeID. 763 // We allow single string localeID.
759 if (typeof locales === 'string') { 764 if (typeof locales === 'string') {
760 %_Call(ArrayPush, seen, canonicalizeLanguageTag(locales)); 765 %_Call(ArrayPush, seen, canonicalizeLanguageTag(locales));
761 return freezeArray(seen); 766 return freezeArray(seen);
762 } 767 }
763 768
764 var o = TO_OBJECT(locales); 769 var o = TO_OBJECT(locales);
765 var len = TO_UINT32(o.length); 770 var len = TO_LENGTH(o.length);
766 771
767 for (var k = 0; k < len; k++) { 772 for (var k = 0; k < len; k++) {
768 if (k in o) { 773 if (k in o) {
769 var value = o[k]; 774 var value = o[k];
770 775
771 var tag = canonicalizeLanguageTag(value); 776 var tag = canonicalizeLanguageTag(value);
772 777
773 if (%_Call(ArrayIndexOf, seen, tag) === -1) { 778 if (%_Call(ArrayIndexOf, seen, tag) === -1) {
774 %_Call(ArrayPush, seen, tag); 779 %_Call(ArrayPush, seen, tag);
775 } 780 }
776 } 781 }
777 } 782 }
778 } 783 }
779 784
780 return freezeArray(seen); 785 return freezeArray(seen);
781 } 786 }
782 787
783 788
784 /** 789 /**
785 * Validates the language tag. Section 2.2.9 of the bcp47 spec 790 * Check the structual Validity of the language tag per ECMA 402 6.2.2:
786 * defines a valid tag. 791 * - Well-formed per RFC 5646 2.1
792 * - There are no duplicate variant subtags
793 * - There are no duplicate singletion (extension) subtags
794 *
795 * One extra-check is done (from RFC 5646 2.2.9): the tag is compared
796 * against the list of grandfathered tags. However, subtags for
797 * primary/extended language, script, region, variant are not checked
798 * against the IANA language subtag registry.
787 * 799 *
788 * ICU is too permissible and lets invalid tags, like 800 * ICU is too permissible and lets invalid tags, like
789 * hant-cmn-cn, through. 801 * hant-cmn-cn, through.
790 * 802 *
791 * Returns false if the language tag is invalid. 803 * Returns false if the language tag is invalid.
792 */ 804 */
793 function isValidLanguageTag(locale) { 805 function isStructuallyValidLanguageTag(locale) {
794 // Check if it's well-formed, including grandfadered tags. 806 // Check if it's well-formed, including grandfadered tags.
795 if (IS_NULL(InternalRegExpMatch(GetLanguageTagRE(), locale))) { 807 if (IS_NULL(InternalRegExpMatch(GetLanguageTagRE(), locale))) {
796 return false; 808 return false;
797 } 809 }
798 810
799 // Just return if it's a x- form. It's all private. 811 // Just return if it's a x- form. It's all private.
800 if (%_Call(StringIndexOf, locale, 'x-') === 0) { 812 if (%_Call(StringIndexOf, locale, 'x-') === 0) {
801 return true; 813 return true;
802 } 814 }
803 815
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 var resolvedAccessor = { 890 var resolvedAccessor = {
879 get() { 891 get() {
880 %IncrementUseCounter(kIntlResolved); 892 %IncrementUseCounter(kIntlResolved);
881 return this[resolvedSymbol]; 893 return this[resolvedSymbol];
882 }, 894 },
883 set(value) { 895 set(value) {
884 this[resolvedSymbol] = value; 896 this[resolvedSymbol] = value;
885 } 897 }
886 }; 898 };
887 899
900 // ECMA 402 section 8.2.1
901 InstallFunction(Intl, 'getCanonicalLocales', function(locales) {
902 if (!IS_UNDEFINED(new.target)) {
903 throw %make_type_error(kOrdinaryFunctionCalledAsConstructor);
904 }
905
906 return initializeLocaleList(locales);
Dan Ehrenberg 2016/08/11 21:07:01 This outputs a frozen Array, but the spec indicate
907 }
908 );
909
888 /** 910 /**
889 * Initializes the given object so it's a valid Collator instance. 911 * Initializes the given object so it's a valid Collator instance.
890 * Useful for subclassing. 912 * Useful for subclassing.
891 */ 913 */
892 function initializeCollator(collator, locales, options) { 914 function initializeCollator(collator, locales, options) {
893 if (%IsInitializedIntlObject(collator)) { 915 if (%IsInitializedIntlObject(collator)) {
894 throw %make_type_error(kReinitializeIntl, "Collator"); 916 throw %make_type_error(kReinitializeIntl, "Collator");
895 } 917 }
896 918
897 if (IS_UNDEFINED(options)) { 919 if (IS_UNDEFINED(options)) {
(...skipping 1348 matching lines...) Expand 10 before | Expand all | Expand 10 after
2246 } 2268 }
2247 ); 2269 );
2248 2270
2249 utils.Export(function(to) { 2271 utils.Export(function(to) {
2250 to.AddBoundMethod = AddBoundMethod; 2272 to.AddBoundMethod = AddBoundMethod;
2251 to.IntlParseDate = IntlParseDate; 2273 to.IntlParseDate = IntlParseDate;
2252 to.IntlParseNumber = IntlParseNumber; 2274 to.IntlParseNumber = IntlParseNumber;
2253 }); 2275 });
2254 2276
2255 }) 2277 })
OLDNEW
« no previous file with comments | « no previous file | src/runtime/runtime-i18n.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698