Java Code Examples for java.util.Locale.toLanguageTag()

The following are Jave code examples for showing how to use toLanguageTag() 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: jdk8u-jdk   File: LocaleEnhanceTest.java   View Source Code Vote up 6 votes
/**
 * Ensure that all current locale ids parse.  Use DateFormat as a proxy
 * for all current locale ids.
 */
public void testCurrentLocales() {
    Locale[] locales = java.text.DateFormat.getAvailableLocales();
    Builder builder = new Builder();

    for (Locale target : locales) {
        String tag = target.toLanguageTag();

        // the tag recreates the original locale,
        // except no_NO_NY
        Locale tagResult = Locale.forLanguageTag(tag);
        if (!target.getVariant().equals("NY")) {
            assertEquals("tagResult", target, tagResult);
        }

        // the builder also recreates the original locale,
        // except ja_JP_JP, th_TH_TH and no_NO_NY
        Locale builderResult = builder.setLocale(target).build();
        if (target.getVariant().length() != 2) {
            assertEquals("builderResult", target, builderResult);
        }
    }
}
 
Example 2
Project: openjdk-jdk10   File: CLDRLocaleProviderAdapter.java   View Source Code Vote up 6 votes
private static Locale getParentLocale(Locale locale) {
    Locale parent = parentLocalesMap.get(locale);

    if (parent == null) {
        String tag = locale.toLanguageTag();
        for (Map.Entry<Locale, String[]> entry : baseMetaInfo.parentLocales().entrySet()) {
            if (Arrays.binarySearch(entry.getValue(), tag) >= 0) {
                parent = entry.getKey();
                break;
            }
        }
        if (parent == null) {
            parent = locale; // non existent marker
        }
        parentLocalesMap.putIfAbsent(locale, parent);
    }

    if (locale.equals(parent)) {
        // means no irregular parent.
        parent = null;
    }

    return parent;
}
 
Example 3
Project: openjdk-jdk10   File: Bug8035133.java   View Source Code Vote up 6 votes
private static void checkLookup(String ranges, String tags,
        String expectedLocale) {

    List<Locale.LanguageRange> priorityList = Locale.LanguageRange
            .parse(ranges);
    List<Locale> localeList = generateLocales(tags);
    Locale loc = Locale.lookup(priorityList, localeList);
    String actualLocale
            = loc.toLanguageTag();

    if (!actualLocale.equals(expectedLocale)) {
        System.err.println("Locale.lookup failed with ranges: " + ranges
                + " Expected: " + expectedLocale
                + " Actual: " + actualLocale);
        err = true;
    }

}
 
Example 4
Project: openjdk-jdk10   File: LocaleEnhanceTest.java   View Source Code Vote up 6 votes
/**
 * Ensure that all current locale ids parse.  Use DateFormat as a proxy
 * for all current locale ids.
 */
public void testCurrentLocales() {
    Locale[] locales = java.text.DateFormat.getAvailableLocales();
    Builder builder = new Builder();

    for (Locale target : locales) {
        String tag = target.toLanguageTag();

        // the tag recreates the original locale,
        // except no_NO_NY
        Locale tagResult = Locale.forLanguageTag(tag);
        if (!target.getVariant().equals("NY")) {
            assertEquals("tagResult", target, tagResult);
        }

        // the builder also recreates the original locale,
        // except ja_JP_JP, th_TH_TH and no_NO_NY
        Locale builderResult = builder.setLocale(target).build();
        if (target.getVariant().length() != 2) {
            assertEquals("builderResult", target, builderResult);
        }
    }
}
 
Example 5
Project: openjdk-jdk10   File: Main.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception {
    int errors = 0;

    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources", locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
Example 6
Project: openjdk-jdk10   File: Main.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception {
    int errors = 0;

    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources", locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
Example 7
Project: openjdk-jdk10   File: Main.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception {
    int errors = 0;
    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources",
                                                     locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            System.out.println("ERROR: " + value + " expected: " + tag);
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
Example 8
Project: openjdk-jdk10   File: Main.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception {
    int errors = 0;

    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources",
                                                     locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
Example 9
Project: openjdk-jdk10   File: Main.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception {
    int errors = 0;
    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources",
                                                     locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println("locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            System.out.println("ERROR: " + value + " expected: " + tag);
            errors++;
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
Example 10
Project: Cypher   File: TOMLSettings.java   View Source Code Vote up 5 votes
@Override
public void setLanguage(Locale language) {
	synchronized (this){
		settingsData.languageTag = language.toLanguageTag();
		save();
	}
}
 
Example 11
Project: openjdk-jdk10   File: LocaleData.java   View Source Code Vote up 5 votes
@Override
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
    String key = baseName + '-' + locale.toLanguageTag();
    List<Locale> candidates = CANDIDATES_MAP.get(key);
    if (candidates == null) {
        LocaleProviderAdapter.Type type = baseName.contains(DOTCLDR) ? CLDR : JRE;
        LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
        candidates = adapter instanceof ResourceBundleBasedAdapter ?
            ((ResourceBundleBasedAdapter)adapter).getCandidateLocales(baseName, locale) :
            defaultControl.getCandidateLocales(baseName, locale);

        // Weed out Locales which are known to have no resource bundles
        int lastDot = baseName.lastIndexOf('.');
        String category = (lastDot >= 0) ? baseName.substring(lastDot + 1) : baseName;
        Set<String> langtags = ((JRELocaleProviderAdapter)adapter).getLanguageTagSet(category);
        if (!langtags.isEmpty()) {
            for (Iterator<Locale> itr = candidates.iterator(); itr.hasNext();) {
                if (!adapter.isSupportedProviderLocale(itr.next(), langtags)) {
                    itr.remove();
                }
            }
        }
        // Force fallback to Locale.ENGLISH for CLDR time zone names support
        if (locale.getLanguage() != "en"
                && type == CLDR && category.equals("TimeZoneNames")) {
            candidates.add(candidates.size() - 1, Locale.ENGLISH);
        }
        CANDIDATES_MAP.putIfAbsent(key, candidates);
    }
    return candidates;
}
 
Example 12
Project: openjdk-jdk10   File: IncludeLocalesPlugin.java   View Source Code Vote up 5 votes
private static Stream<String> localeToTags(Locale loc) {
    Objects.requireNonNull(loc);

    String tag = loc.toLanguageTag();
    List<String> tags = null;

    switch (loc.getLanguage()) {
        // ISO3166 compatibility
        case "iw":
            tags = List.of(tag, tag.replaceFirst("^he", "iw"));
            break;
        case "in":
            tags = List.of(tag, tag.replaceFirst("^id", "in"));
            break;
        case "ji":
            tags = List.of(tag, tag.replaceFirst("^yi", "ji"));
            break;

        // Special COMPAT provider locales
        case "ja":
            if (loc.getCountry() == "JP") {
                tags = List.of(tag, jaJPJPTag);
            }
            break;
        case "no":
        case "nn":
            if (loc.getCountry() == "NO") {
                tags = List.of(tag, noNONYTag);
            }
            break;
        case "th":
            if (loc.getCountry() == "TH") {
                tags = List.of(tag, thTHTHTag);
            }
            break;
    }

    return tags == null ? List.of(tag).stream() : tags.stream();
}
 
Example 13
Project: openjdk-jdk10   File: Bug8159420.java   View Source Code Vote up 5 votes
private static void testLookup() {
    boolean error = false;
    String ranges = "hi-IN, itc-Ital";
    String tags = "hi-IN, itc-Ital";
    List<LanguageRange> priorityList = LanguageRange.parse(ranges);
    List<Locale> localeList = generateLocales(tags);
    Locale actualLocale
            = Locale.lookup(priorityList, localeList);
    String actualLocaleString = "";

    if (actualLocale != null) {
        actualLocaleString = actualLocale.toLanguageTag();
    } else {
        error = true;
    }

    String expectedLocale = "hi-IN";

    if (!expectedLocale.equals(actualLocaleString)) {
        error = true;
    }

    if (error) {
        System.err.println("testLookup() failed on language ranges:"
                + " [" + ranges + "] and language tags: [" + tags + "]");
        err = true;
    }

}
 
Example 14
Project: openjdk-jdk10   File: MyResourcesProvider.java   View Source Code Vote up 5 votes
@Override
public ResourceBundle getBundle(String baseName, Locale locale) {
    ResourceBundle rb = super.getBundle(baseName, locale);
    String tag = locale.toLanguageTag();
    if (tag.equals("und")) {
        tag = "ROOT"; // to a human friendly name
    }
    System.out.printf("    MyResourcesProvider.getBundle(%s, %s)%n         -> %s%n",
                      baseName, tag, rb);
    return rb;
}
 
Example 15
Project: openjdk-jdk10   File: MyResourcesProvider.java   View Source Code Vote up 5 votes
@Override
public ResourceBundle getBundle(String baseName, Locale locale) {
    ResourceBundle rb = super.getBundle(baseName, locale);
    String tag = locale.toLanguageTag();
    if (tag.equals("und")) {
        tag = "ROOT"; // to a human friendly name
    }
    System.out.printf("    MyResourcesProvider.getBundle(%s, %s)%n         -> %s%n",
                      baseName, tag, rb);
    return rb;
}
 
Example 16
Project: openjdk-jdk10   File: Main.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    int errors = 0;
    for (String loctag : args) {
        Locale locale = Locale.forLanguageTag(loctag);
        if (locale.equals(Locale.ROOT)) {
            continue;
        }
        ResourceBundle rb = ResourceBundle.getBundle("jdk.test.resources.MyResources",
                                                     locale);
        String tag = locale.toLanguageTag(); // normalized
        String value = rb.getString("key");
        System.out.println(rb.getBaseBundleName() + ": locale = " + tag + ", value = " + value);
        if (!value.startsWith(tag + ':')) {
            System.out.println("ERROR: " + value + " expected: " + tag);
            errors++;
        }

        // inaccessible bundles
        try {
            ResourceBundle.getBundle("jdk.test.internal.resources.Foo", locale);
            System.out.println("ERROR: jdk.test.internal.resources.Foo should not be accessible");
            errors++;
        } catch (MissingResourceException e) {
            e.printStackTrace();

            Throwable cause = e.getCause();
            System.out.println("Expected: " +
                (cause != null ? cause.getMessage() : e.getMessage()));
        }
    }
    if (errors > 0) {
        throw new RuntimeException(errors + " errors");
    }
}
 
Example 17
Project: roxana   File: MessageFullyCreator.java   View Source Code Vote up 5 votes
@Override
public MessageFully create(MessageMapper mapper, Collection<Parameter> parameters) {
	Locale locale = this.getTranslator().getDefaultLocale();
	String language = locale.toLanguageTag();
	String translation = this.getTranslator().translate(mapper.getKey(), locale, parameters);
	return new MessageFully(mapper.getKey(), mapper.getSeverity(), language, translation, parameters);
}
 
Example 18
Project: jdk8u-jdk   File: LocaleEnhanceTest.java   View Source Code Vote up 4 votes
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 = {
        {"ja-JP-x-lvariant-JP", "ja-JP-u-ca-japanese-x-lvariant-JP"},   // special case 1
        {"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"},
        {"th-TH-x-lvariant-TH", "th-TH-u-nu-thai-x-lvariant-TH"},   // special case 2
        {"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);
    }
}
 
Example 19
Project: openjdk-jdk10   File: LocaleEnhanceTest.java   View Source Code Vote up 4 votes
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 = {
        {"ja-JP-x-lvariant-JP", "ja-JP-u-ca-japanese-x-lvariant-JP"},   // special case 1
        {"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"},
        {"th-TH-x-lvariant-TH", "th-TH-u-nu-thai-x-lvariant-TH"},   // special case 2
        {"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);
    }
}