michael@0: // Copyright 2011-2012 Norbert Lindenberg. All rights reserved. michael@0: // Copyright 2012 Mozilla Corporation. All rights reserved. michael@0: // This code is governed by the BSD license found in the LICENSE file. michael@0: michael@0: /** michael@0: * @description Tests that language tags are canonicalized in return values. michael@0: * @author Norbert Lindenberg michael@0: */ michael@0: michael@0: $INCLUDE("testIntl.js"); michael@0: michael@0: var canonicalizedTags = { michael@0: "de": ["de"], michael@0: "de-DE": ["de-DE", "de"], michael@0: "DE-de": ["de-DE", "de"], michael@0: "cmn": ["cmn"], michael@0: "CMN-hANS": ["cmn-Hans", "cmn"], michael@0: "cmn-hans-cn": ["cmn-Hans-CN", "cmn-Hans", "cmn"], michael@0: "es-419": ["es-419", "es"], michael@0: "es-419-u-nu-latn": ["es-419-u-nu-latn", "es-419", "es", "es-u-nu-latn"], michael@0: // -u-ca is incomplete, so it will not show up in resolvedOptions().locale michael@0: "cmn-hans-cn-u-ca-t-ca-x-t-u": ["cmn-Hans-CN-t-ca-u-ca-x-t-u", "cmn-Hans-CN-t-ca-x-t-u", "cmn-Hans-CN-t-ca-x-t", "cmn-Hans-CN-t-ca", "cmn-Hans-CN", "cmn-Hans", "cmn"], michael@0: "enochian-enochian": ["enochian-enochian", "enochian"], michael@0: "de-gregory-u-ca-gregory": ["de-gregory-u-ca-gregory", "de-gregory", "de-u-ca-gregory", "de"], michael@0: "no-nyn": ["nn"], michael@0: "i-klingon": ["tlh"], michael@0: "sgn-GR": ["gss"], michael@0: "ji": ["yi"], michael@0: "de-DD": ["de-DE", "de"], michael@0: "zh-hak-CN": ["hak-CN", "hak"], michael@0: "sgn-ils": ["ils"], michael@0: "in": ["id"], michael@0: "x-foo": ["x-foo"] michael@0: }; michael@0: michael@0: // make sure the data above is correct michael@0: Object.getOwnPropertyNames(canonicalizedTags).forEach(function (tag) { michael@0: canonicalizedTags[tag].forEach(function (canonicalTag) { michael@0: if (!isCanonicalizedStructurallyValidLanguageTag(canonicalTag)) { michael@0: $ERROR("Test data \"" + canonicalTag + "\" is not canonicalized and structurally valid language tag."); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: // now the actual test michael@0: testWithIntlConstructors(function (Constructor) { michael@0: var defaultLocale = new Constructor().resolvedOptions().locale; michael@0: Object.getOwnPropertyNames(canonicalizedTags).forEach(function (tag) { michael@0: // use lookup locale matcher to keep the set of possible return values predictable michael@0: michael@0: // Variant 1: construct an object and see whether its locale is canonicalized. michael@0: // In this variant, shortened forms or the default locale may be returned michael@0: var object = new Constructor([tag], {localeMatcher: "lookup"}); michael@0: var locale = object.resolvedOptions().locale; michael@0: if (canonicalizedTags[tag].indexOf(locale) === -1 && locale !== defaultLocale) { michael@0: $ERROR("For " + tag + " got " + locale + "; expected one of " + michael@0: canonicalizedTags[tag].join(", ") + "."); michael@0: } michael@0: michael@0: // Variant 2: get the supported locales. If the tag is supported, it should be returned canonicalized but unshortened michael@0: var supported = Constructor.supportedLocalesOf([tag]); michael@0: if (supported.length > 0 && supported[0] !== canonicalizedTags[tag][0]) { michael@0: $ERROR("For " + tag + " got " + supported[0] + "; expected " + michael@0: canonicalizedTags[tag][0] + "."); michael@0: } michael@0: }); michael@0: return true; michael@0: }); michael@0: