Java Code Examples for java.util.Locale#Builder

The following examples show how to use java.util.Locale#Builder . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: Tomcat8-Source-Read   File: TestAcceptLanguage.java    License: MIT License 6 votes vote down vote up
@Test
public void bug56848() throws Exception {
    List<AcceptLanguage> actual =
            AcceptLanguage.parse(new StringReader("zh-hant-CN;q=0.5,zh-hans-TW;q=0.05"));

    Assert.assertEquals(2, actual.size());

    Locale.Builder b = new Locale.Builder();
    b.setLanguage("zh").setRegion("CN").setScript("hant");
    Locale l1 = b.build();

    b.clear().setLanguage("zh").setRegion("TW").setScript("hans");
    Locale l2 = b.build();

    Assert.assertEquals(l1, actual.get(0).getLocale());
    Assert.assertEquals(Q0_500, actual.get(0).getQuality(), 0.0001);
    Assert.assertEquals(l2, actual.get(1).getLocale());
    Assert.assertEquals(Q0_050, actual.get(1).getQuality(), 0.0001);
}
 
Example 2
private static Locale getCalendarLocale(Locale locale) {
    int calid = getCalendarID(locale.toLanguageTag());
    if (calid > 0 && calid < calIDToLDML.length) {
        Locale.Builder lb = new Locale.Builder();
        String[] caltype = calIDToLDML[calid].split("_");
        if (caltype.length > 1) {
            lb.setLocale(Locale.forLanguageTag(caltype[1]));
        } else {
            lb.setLocale(locale);
        }
        lb.setUnicodeLocaleKeyword("ca", caltype[0]);
        return lb.build();
    }

    return locale;
}
 
Example 3
private static Locale getCalendarLocale(Locale locale) {
    int calid = getCalendarID(locale.toLanguageTag());
    if (calid > 0 && calid < calIDToLDML.length) {
        Locale.Builder lb = new Locale.Builder();
        String[] caltype = calIDToLDML[calid].split("_");
        if (caltype.length > 1) {
            lb.setLocale(Locale.forLanguageTag(caltype[1]));
        } else {
            lb.setLocale(locale);
        }
        lb.setUnicodeLocaleKeyword("ca", caltype[0]);
        return lb.build();
    }

    return locale;
}
 
Example 4
private static Locale getCalendarLocale(Locale locale) {
    int calid = getCalendarID(locale.toLanguageTag());
    if (calid > 0 && calid < calIDToLDML.length) {
        Locale.Builder lb = new Locale.Builder();
        String[] caltype = calIDToLDML[calid].split("_");
        if (caltype.length > 1) {
            lb.setLocale(Locale.forLanguageTag(caltype[1]));
        } else {
            lb.setLocale(locale);
        }
        lb.setUnicodeLocaleKeyword("ca", caltype[0]);
        return lb.build();
    }

    return locale;
}
 
Example 5
private static Locale getNumberLocale(Locale src) {
    if (JRELocaleConstants.TH_TH.equals(src)) {
        if (isNativeDigit("th-TH")) {
            Locale.Builder lb = new Locale.Builder().setLocale(src);
            lb.setUnicodeLocaleKeyword("nu", "thai");
            return lb.build();
        }
    }

    return src;
}
 
Example 6
@Test(expectedExceptions=DateTimeException.class)
public void test_lookupLocale() {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", "xxx");

    Locale locale = builder.build();
    Chronology.ofLocale(locale);
}
 
Example 7
Source Project: j2objc   File: TCKChronology.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected=DateTimeException.class)
public void test_lookupLocale() {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", "xxx");

    Locale locale = builder.build();
    Chronology.ofLocale(locale);
}
 
Example 8
@Test(expectedExceptions=DateTimeException.class)
public void test_lookupLocale() {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", "xxx");

    Locale locale = builder.build();
    Chronology.ofLocale(locale);
}
 
Example 9
private static Locale getNumberLocale(Locale src) {
    if (JRELocaleConstants.TH_TH.equals(src)) {
        if (isNativeDigit("th-TH")) {
            Locale.Builder lb = new Locale.Builder().setLocale(src);
            lb.setUnicodeLocaleKeyword("nu", "thai");
            return lb.build();
        }
    }

    return src;
}
 
Example 10
@Test(dataProvider = "calendarsystemtype")
public void test_lookupLocale(Chronology chrono, String calendarType) {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", calendarType);
    Locale locale = builder.build();
    assertEquals(Chronology.ofLocale(locale), chrono);
}
 
Example 11
@Test(dataProvider = "calendarsystemtype")
public void test_lookupLocale(Chronology chrono, String calendarType) {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", calendarType);
    Locale locale = builder.build();
    assertEquals(Chronology.ofLocale(locale), chrono);
}
 
Example 12
/**
 * Test lookup by calendarType of each chronology.
 * Verify that the calendar can be found by {@link java.time.chrono.Chronology#ofLocale}.
 */
@Test
public void test_ofLocaleByType() {
    // Test that all available chronologies can be successfully found using ofLocale
    Set<Chronology> chronos = Chronology.getAvailableChronologies();
    for (Chronology chrono : chronos) {
        Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
        builder.setUnicodeLocaleKeyword("ca", chrono.getCalendarType());
        Locale locale = builder.build();
        assertEquals(Chronology.ofLocale(locale), chrono, "Lookup by type");
    }
}
 
Example 13
@Test(expectedExceptions=DateTimeException.class)
public void test_lookupLocale() {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", "xxx");

    Locale locale = builder.build();
    Chronology.ofLocale(locale);
}
 
Example 14
public static void main(String[] args) {
    Locale reservedLocale = Locale.getDefault();
    try {
        Locale.Builder builder = new Locale.Builder();

        base = builder.setLanguage(System.getProperty("user.language", ""))
              .setScript(System.getProperty("user.script", ""))
              .setRegion(System.getProperty("user.country", ""))
              .setVariant(System.getProperty("user.variant", "")).build();
        disp = builder.setLanguage(
                System.getProperty("user.language.display",
                            Locale.getDefault().getLanguage()))
                    .setScript(System.getProperty("user.script.display",
                            Locale.getDefault().getScript()))
                    .setRegion(System.getProperty("user.country.display",
                            Locale.getDefault().getCountry()))
                    .setVariant(System.getProperty("user.variant.display",
                            Locale.getDefault().getVariant())).build();
        fmt = builder.setLanguage(System.getProperty("user.language.format",
                            Locale.getDefault().getLanguage()))
                   .setScript(System.getProperty("user.script.format",
                            Locale.getDefault().getScript()))
                   .setRegion(System.getProperty("user.country.format",
                            Locale.getDefault().getCountry()))
                   .setVariant(System.getProperty("user.variant.format",
                              Locale.getDefault().getVariant())).build();
        checkDefault();
        testGetSetDefault();
        testBug7079486();
    } finally {
        // restore the reserved locale
        Locale.setDefault(reservedLocale);
    }
}
 
Example 15
/**
 * Test lookup by calendarType of each chronology.
 * Verify that the calendar can be found by {@link java.time.chrono.Chronology#ofLocale}.
 */
@Test
public void test_ofLocaleByType() {
    // Test that all available chronologies can be successfully found using ofLocale
    Set<Chronology> chronos = Chronology.getAvailableChronologies();
    for (Chronology chrono : chronos) {
        Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
        builder.setUnicodeLocaleKeyword("ca", chrono.getCalendarType());
        Locale locale = builder.build();
        assertEquals(Chronology.ofLocale(locale), chrono, "Lookup by type");
    }
}
 
Example 16
public static void main(String[] args) {
    Locale reservedLocale = Locale.getDefault();
    if (TestUtils.hasSpecialVariant(reservedLocale)) {
        System.out.println("Skipping this test because locale is " + reservedLocale);
        return;
    }

    try {
        Locale.Builder builder = new Locale.Builder();

        base = builder.setLanguage(System.getProperty("user.language", ""))
              .setScript(System.getProperty("user.script", ""))
              .setRegion(System.getProperty("user.country", ""))
              .setVariant(System.getProperty("user.variant", "")).build();
        disp = builder.setLanguage(
                System.getProperty("user.language.display",
                            Locale.getDefault().getLanguage()))
                    .setScript(System.getProperty("user.script.display",
                            Locale.getDefault().getScript()))
                    .setRegion(System.getProperty("user.country.display",
                            Locale.getDefault().getCountry()))
                    .setVariant(System.getProperty("user.variant.display",
                            Locale.getDefault().getVariant())).build();
        fmt = builder.setLanguage(System.getProperty("user.language.format",
                            Locale.getDefault().getLanguage()))
                   .setScript(System.getProperty("user.script.format",
                            Locale.getDefault().getScript()))
                   .setRegion(System.getProperty("user.country.format",
                            Locale.getDefault().getCountry()))
                   .setVariant(System.getProperty("user.variant.format",
                              Locale.getDefault().getVariant())).build();
        checkDefault();
        testGetSetDefault();
        testBug7079486();
    } finally {
        // restore the reserved locale
        Locale.setDefault(reservedLocale);
    }
}
 
Example 17
@Test(dataProvider = "calendarsystemtype")
public void test_lookupLocale(Chronology chrono, String calendarType) {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", calendarType);
    Locale locale = builder.build();
    assertEquals(Chronology.ofLocale(locale), chrono);
}
 
Example 18
public static void main(String[] args) {
    Locale reservedLocale = Locale.getDefault();
    try {
        Locale.Builder builder = new Locale.Builder();

        base = builder.setLanguage(System.getProperty("user.language", ""))
              .setScript(System.getProperty("user.script", ""))
              .setRegion(System.getProperty("user.country", ""))
              .setVariant(System.getProperty("user.variant", "")).build();
        disp = builder.setLanguage(
                System.getProperty("user.language.display",
                            Locale.getDefault().getLanguage()))
                    .setScript(System.getProperty("user.script.display",
                            Locale.getDefault().getScript()))
                    .setRegion(System.getProperty("user.country.display",
                            Locale.getDefault().getCountry()))
                    .setVariant(System.getProperty("user.variant.display",
                            Locale.getDefault().getVariant())).build();
        fmt = builder.setLanguage(System.getProperty("user.language.format",
                            Locale.getDefault().getLanguage()))
                   .setScript(System.getProperty("user.script.format",
                            Locale.getDefault().getScript()))
                   .setRegion(System.getProperty("user.country.format",
                            Locale.getDefault().getCountry()))
                   .setVariant(System.getProperty("user.variant.format",
                              Locale.getDefault().getVariant())).build();
        checkDefault();
        testGetSetDefault();
        testBug7079486();
    } finally {
        // restore the reserved locale
        Locale.setDefault(reservedLocale);
    }
}
 
Example 19
@Test(dataProvider = "calendarsystemtype")
public void test_lookupLocale(Chronology chrono, String calendarType) {
    Locale.Builder builder = new Locale.Builder().setLanguage("en").setRegion("CA");
    builder.setUnicodeLocaleKeyword("ca", calendarType);
    Locale locale = builder.build();
    assertEquals(Chronology.ofLocale(locale), chrono);
}
 
Example 20
Source Project: j2objc   File: LocaleTest.java    License: Apache License 2.0 4 votes vote down vote up
public void test_toString() {
    Locale.Builder b = new Locale.Builder();

    // Empty builder.
    Locale l = b.build();
    assertEquals("", l.toString());

    // Only language.
    b = new Locale.Builder();
    b.setLanguage("en");
    assertEquals("en", b.build().toString());

    // Only region
    b = new Locale.Builder();
    b.setRegion("US");
    assertEquals("_US", b.build().toString());

    // Language & Region
    b = new Locale.Builder();
    b.setLanguage("en").setRegion("US");
    assertEquals("en_US", b.build().toString());

    // Language & Script
    b = new Locale.Builder();
    b.setLanguage("en").setScript("Latn");
    assertEquals("en__#Latn", b.build().toString());

    // Language & Variant
    b = new Locale.Builder();
    b.setLanguage("en").setVariant("foooo");
    assertEquals("en__foooo", b.build().toString());

    // Language / script & country
    b = new Locale.Builder();
    b.setLanguage("en").setScript("Latn").setRegion("US");
    assertEquals("en_US_#Latn", b.build().toString());

    // Language / script & variant
    b = new Locale.Builder();
    b.setLanguage("en").setScript("Latn").setVariant("foooo");
    assertEquals("en__foooo_#Latn", b.build().toString());

    // Language / script / country / variant.
    b = new Locale.Builder();
    b.setLanguage("en").setScript("Latn").setVariant("foooo").setRegion("US");
    assertEquals("en_US_foooo_#Latn", b.build().toString());

    // Language / extension
    b = new Locale.Builder();
    b.setLanguage("en").setExtension('x', "fooo-baar");
    assertEquals("en__#x-fooo-baar", b.build().toString());
}