Example usage for org.apache.commons.lang3 LocaleUtils countriesByLanguage

List of usage examples for org.apache.commons.lang3 LocaleUtils countriesByLanguage

Introduction

In this page you can find the example usage for org.apache.commons.lang3 LocaleUtils countriesByLanguage.

Prototype

public static List<Locale> countriesByLanguage(final String languageCode) 

Source Link

Document

Obtains the list of countries supported for a given language.

This method takes a language code and searches to find the countries available for that language.

Usage

From source file:org.tinymediamanager.core.Utils.java

/**
 * Gets a correct Locale (language + country) from given language.
 * //from ww w  . j  av  a  2 s  . c  o  m
 * @param language
 *          as 2char
 * @return Locale
 */
public static Locale getLocaleFromLanguage(String language) {
    if (StringUtils.isBlank(language)) {
        return Locale.getDefault();
    }
    // do we have a newer locale settings style?
    if (language.length() > 2) {
        return LocaleUtils.toLocale(language);
    }
    if (language.equalsIgnoreCase("en")) {
        return new Locale("en", "US"); // don't mess around; at least fixtate this
    }
    Locale l = null;
    List<Locale> countries = LocaleUtils.countriesByLanguage(language.toLowerCase(Locale.ROOT));
    for (Locale locale : countries) {
        if (locale.getCountry().equalsIgnoreCase(language)) {
            // map to main countries; de->de_DE (and not de_CH)
            l = locale;
        }
    }
    if (l == null && countries.size() > 0) {
        // well, take the first one
        l = countries.get(0);
    }

    return l;
}

From source file:org.tinymediamanager.scraper.util.UrlUtil.java

License:asdf

/**
 * Gets a correct Locale (language + country) from given language.
 * /*from   w w w  . j  ava  2s. c  o m*/
 * @param language
 *          as 2char
 * @return Locale
 */
public static Locale getLocaleFromLanguage(String language) {
    if (language == null || language.isEmpty()) {
        return null;
    }
    if (language.equalsIgnoreCase("en")) {
        return new Locale("en", "US"); // don't mess around; at least fixtate this
    }
    Locale l = null;
    List<Locale> countries = LocaleUtils.countriesByLanguage(language.toLowerCase(Locale.ROOT));
    for (Locale locale : countries) {
        if (locale.getCountry().equalsIgnoreCase(language)) {
            // map to main countries; de->de_DE (and not de_CH)
            l = locale;
        }
    }
    if (l == null && countries.size() > 0) {
        // well, take the first one
        l = countries.get(0);
    }

    return l;
}