Search in sources :

Example 1 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleServiceProviderPool method getLookupLocale.

/**
     * Returns an instance of Locale used for service look up.
     * The result Locale has no extensions except for ja_JP_JP
     * and th_TH_TH
     *
     * @param locale the locale
     * @return the locale used for service look up
     */
static Locale getLookupLocale(Locale locale) {
    Locale lookupLocale = locale;
    if (locale.hasExtensions() && !locale.equals(JRELocaleConstants.JA_JP_JP) && !locale.equals(JRELocaleConstants.TH_TH_TH)) {
        // remove extensions
        Builder locbld = new Builder();
        try {
            locbld.setLocale(locale);
            locbld.clearExtensions();
            lookupLocale = locbld.build();
        } catch (IllformedLocaleException e) {
            // A Locale with non-empty extensions
            // should have well-formed fields except
            // for ja_JP_JP and th_TH_TH. Therefore,
            // it should never enter in this catch clause.
            config(LocaleServiceProviderPool.class, "A locale(" + locale + ") has non-empty extensions, but has illformed fields.");
            // Fallback - script field will be lost.
            lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant());
        }
    }
    return lookupLocale;
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder) IllformedLocaleException(java.util.IllformedLocaleException)

Example 2 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testBug7002320.

public void testBug7002320() {
    // forLanguageTag() and Builder.setLanguageTag(String)
    // should add a location extension for following two cases.
    //
    // 1. language/country are "ja"/"JP" and the resolved variant (x-lvariant-*)
    //    is exactly "JP" and no BCP 47 extensions are available, then add
    //    a Unicode locale extension "ca-japanese".
    // 2. language/country are "th"/"TH" and the resolved variant is exactly
    //    "TH" and no BCP 47 extensions are available, then add a Unicode locale
    //    extension "nu-thai".
    //
    String[][] testdata = { // special case 1
    { "ja-JP-x-lvariant-JP", "ja-JP-u-ca-japanese-x-lvariant-JP" }, { "ja-JP-x-lvariant-JP-XXX" }, { "ja-JP-u-ca-japanese-x-lvariant-JP" }, { "ja-JP-u-ca-gregory-x-lvariant-JP" }, { "ja-JP-u-cu-jpy-x-lvariant-JP" }, { "ja-x-lvariant-JP" }, // special case 2
    { "th-TH-x-lvariant-TH", "th-TH-u-nu-thai-x-lvariant-TH" }, { "th-TH-u-nu-thai-x-lvariant-TH" }, { "en-US-x-lvariant-JP" } };
    Builder bldr = new Builder();
    for (String[] data : testdata) {
        String in = data[0];
        String expected = (data.length == 1) ? data[0] : data[1];
        // forLanguageTag
        Locale loc = Locale.forLanguageTag(in);
        String out = loc.toLanguageTag();
        assertEquals("Language tag roundtrip by forLanguageTag with input: " + in, expected, out);
        // setLanguageTag
        bldr.clear();
        bldr.setLanguageTag(in);
        loc = bldr.build();
        out = loc.toLanguageTag();
        assertEquals("Language tag roundtrip by Builder.setLanguageTag with input: " + in, expected, out);
    }
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 3 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testBuildersetUnicodeLocaleKeyword.

public void testBuildersetUnicodeLocaleKeyword() {
    // Note: most behavior is tested in testBuilderSetExtension
    Builder builder = new Builder();
    Locale locale = builder.setUnicodeLocaleKeyword("co", "japanese").setUnicodeLocaleKeyword("nu", "thai").build();
    assertEquals("co", "japanese", locale.getUnicodeLocaleType("co"));
    assertEquals("nu", "thai", locale.getUnicodeLocaleType("nu"));
    assertEquals("keys", 2, locale.getUnicodeLocaleKeys().size());
    // can clear a keyword by setting to null, others remain
    String result = builder.setUnicodeLocaleKeyword("co", null).build().toLanguageTag();
    assertEquals("empty co", "und-u-nu-thai", result);
    // locale keyword extension goes when all keywords are gone
    result = builder.setUnicodeLocaleKeyword("nu", null).build().toLanguageTag();
    assertEquals("empty nu", "und", result);
    // locale keywords are ordered independent of order of addition
    result = builder.setUnicodeLocaleKeyword("zz", "012").setUnicodeLocaleKeyword("aa", "345").build().toLanguageTag();
    assertEquals("reordered", "und-u-aa-345-zz-012", result);
    // null keyword throws NPE
    new BuilderNPE("keyword") {

        public void call() {
            b.setUnicodeLocaleKeyword(null, "thai");
        }
    };
    // well-formed keywords are two alphanum
    new BuilderILE("a", "abc") {

        public void call() {
            b.setUnicodeLocaleKeyword(arg, "value");
        }
    };
    // well-formed values are 3-8 alphanum
    new BuilderILE("ab", "abcdefghi") {

        public void call() {
            b.setUnicodeLocaleKeyword("ab", arg);
        }
    };
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 4 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testBuilderSetExtension.

public void testBuilderSetExtension() {
    // upper case characters are normalized to lower case
    final char sourceKey = 'a';
    final String sourceValue = "aB-aBcdefgh-12-12345678";
    String target = "ab-abcdefgh-12-12345678";
    Builder builder = new Builder();
    String result = builder.setExtension(sourceKey, sourceValue).build().getExtension(sourceKey);
    assertEquals("extension", target, result);
    // setting with empty resets
    result = builder.setExtension(sourceKey, sourceValue).setExtension(sourceKey, "").build().getExtension(sourceKey);
    assertEquals("empty", null, result);
    // setting with null also resets
    result = builder.setExtension(sourceKey, sourceValue).setExtension(sourceKey, null).build().getExtension(sourceKey);
    assertEquals("null", null, result);
    // ill-formed extension keys throw IAE
    // must be in [0-9a-ZA-Z]
    new BuilderILE("$") {

        public void call() {
            b.setExtension('$', sourceValue);
        }
    };
    // each segment of value must be 2-8 alphanum
    new BuilderILE("ab-cd-123456789") {

        public void call() {
            b.setExtension(sourceKey, arg);
        }
    };
    // no multiple hyphens.
    new BuilderILE("ab--cd") {

        public void call() {
            b.setExtension(sourceKey, arg);
        }
    };
    // locale extension key has special handling
    Locale locale = builder.setExtension('u', "co-japanese").build();
    assertEquals("locale extension", "japanese", locale.getUnicodeLocaleType("co"));
    // locale extension has same behavior with set locale keyword
    Locale locale2 = builder.setUnicodeLocaleKeyword("co", "japanese").build();
    assertEquals("locales with extension", locale, locale2);
    // setting locale extension overrides all previous calls to setLocaleKeyword
    Locale locale3 = builder.setExtension('u', "xxx-nu-thai").build();
    assertEquals("remove co", null, locale3.getUnicodeLocaleType("co"));
    assertEquals("override thai", "thai", locale3.getUnicodeLocaleType("nu"));
    assertEquals("override attribute", 1, locale3.getUnicodeLocaleAttributes().size());
    // setting locale keyword extends values already set by the locale extension
    Locale locale4 = builder.setUnicodeLocaleKeyword("co", "japanese").build();
    assertEquals("extend", "japanese", locale4.getUnicodeLocaleType("co"));
    assertEquals("extend", "thai", locale4.getUnicodeLocaleType("nu"));
    // locale extension subtags are reordered
    result = builder.clear().setExtension('u', "456-123-zz-123-yy-456-xx-789").build().toLanguageTag();
    assertEquals("reorder", "und-u-123-456-xx-789-yy-456-zz-123", result);
    // multiple keyword types
    result = builder.clear().setExtension('u', "nu-thai-foobar").build().getUnicodeLocaleType("nu");
    assertEquals("multiple types", "thai-foobar", result);
    // redundant locale extensions are ignored
    result = builder.clear().setExtension('u', "nu-thai-NU-chinese-xx-1234").build().toLanguageTag();
    assertEquals("duplicate keys", "und-u-nu-thai-xx-1234", result);
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder)

Example 5 with Builder

use of java.util.Locale.Builder in project jdk8u_jdk by JetBrains.

the class LocaleEnhanceTest method testBuilderSetLanguageTag.

public void testBuilderSetLanguageTag() {
    String source = "eN-LaTn-Us-NewYork-A-Xx-B-Yy-X-1-2-3";
    String target = "en-Latn-US-NewYork-a-xx-b-yy-x-1-2-3";
    Builder builder = new Builder();
    String result = builder.setLanguageTag(source).build().toLanguageTag();
    assertEquals("language", target, result);
    // redundant extensions cause a failure
    new BuilderILE() {

        public void call() {
            b.setLanguageTag("und-a-xx-yy-b-ww-A-00-11-c-vv");
        }
    };
    // redundant Unicode locale extension keys within an Unicode locale extension cause a failure
    new BuilderILE() {

        public void call() {
            b.setLanguageTag("und-u-nu-thai-NU-chinese-xx-1234");
        }
    };
}
Also used : Builder(java.util.Locale.Builder)

Aggregations

Builder (java.util.Locale.Builder)18 Locale (java.util.Locale)12 IllformedLocaleException (java.util.IllformedLocaleException)1