Java Code Examples for java.util.Locale.Builder

The following are Jave code examples for showing how to use Builder of the java.util.Locale class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: OpenJSharp   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 6 votes
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 2
Project: google-speech-api-android   File: SupportedLanguageConverter.java   View Source Code Vote up 6 votes
private static Locale createLocale(@NonNull String language, @Nullable String region, @Nullable String script) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Locale.Builder localeBuilder = new Locale.Builder();
        localeBuilder.setLanguage(language);
        if (region != null) {
            localeBuilder.setRegion(region);
        }
        if (script != null) {
            localeBuilder.setScript(script);
        }
        return localeBuilder.build();
    } else {
        if (region == null) {
            return new Locale(language);
        } else if (script == null) {
            return new Locale(language, region);
        } else {
            return new Locale(language, region, script);
        }
    }
}
 
Example 3
Project: jdk8u-jdk   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 6 votes
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
Project: openjdk-jdk10   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 6 votes
private static Locale getCalendarLocale(Locale locale) {
    int calid = getCalendarID(stripVariantAndExtensions(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
Project: OpenJSharp   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 5 votes
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
Project: jdk8u-jdk   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 5 votes
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 7
Project: jdk8u-jdk   File: TCKChronology.java   View Source Code Vote up 5 votes
@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 8
Project: jdk8u-jdk   File: TCKChronology.java   View Source Code Vote up 5 votes
/**
 * 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 9
Project: jdk8u-jdk   File: TCKChronology.java   View Source Code Vote up 5 votes
@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 10
Project: jdk8u-jdk   File: TestExampleCode.java   View Source Code Vote up 5 votes
@Test(dataProvider= "HijrahTypeNames")
public void test_HijrahTypeViaLocale(String calendarId, String calendarType) {
    Locale.Builder builder = new Locale.Builder();
    builder.setLanguage("en").setRegion("US");
    builder.setUnicodeLocaleKeyword("ca", calendarType);
    Locale locale = builder.build();
    Chronology chrono = Chronology.ofLocale(locale);
    System.out.printf(" Locale language tag: %s, Chronology ID: %s, type: %s%n",
            locale.toLanguageTag(), chrono, chrono.getCalendarType());
    Chronology expected = Chronology.of(calendarId);
    assertEquals(chrono, expected, "Expected chronology not found");
}
 
Example 11
Project: jdk8u-jdk   File: LocaleCategory.java   View Source Code Vote up 5 votes
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 12
Project: openjdk-jdk10   File: HostLocaleProviderAdapterImpl.java   View Source Code Vote up 5 votes
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 13
Project: openjdk-jdk10   File: TCKChronology.java   View Source Code Vote up 5 votes
@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 14
Project: openjdk-jdk10   File: TCKChronology.java   View Source Code Vote up 5 votes
/**
 * 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 15
Project: openjdk-jdk10   File: TCKChronology.java   View Source Code Vote up 5 votes
@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 16
Project: openjdk-jdk10   File: TestExampleCode.java   View Source Code Vote up 5 votes
@Test(dataProvider= "HijrahTypeNames")
public void test_HijrahTypeViaLocale(String calendarId, String calendarType) {
    Locale.Builder builder = new Locale.Builder();
    builder.setLanguage("en").setRegion("US");
    builder.setUnicodeLocaleKeyword("ca", calendarType);
    Locale locale = builder.build();
    Chronology chrono = Chronology.ofLocale(locale);
    System.out.printf(" Locale language tag: %s, Chronology ID: %s, type: %s%n",
            locale.toLanguageTag(), chrono, chrono.getCalendarType());
    Chronology expected = Chronology.of(calendarId);
    assertEquals(chrono, expected, "Expected chronology not found");
}
 
Example 17
Project: openjdk-jdk10   File: LocaleCategory.java   View Source Code Vote up 5 votes
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);
    }
}