Example usage for java.util Locale Locale

List of usage examples for java.util Locale Locale

Introduction

In this page you can find the example usage for java.util Locale Locale.

Prototype

public Locale(String language, String country) 

Source Link

Document

Construct a locale from language and country.

Usage

From source file:com.liferay.util.LocaleUtil.java

public static Locale fromLanguageId(String languageId) {
    Locale locale = null;//from w  w  w.ja va 2 s  . com

    try {
        int pos = languageId.indexOf(StringPool.UNDERLINE);

        String languageCode = languageId.substring(0, pos);
        String countryCode = languageId.substring(pos + 1, languageId.length());

        locale = new Locale(languageCode, countryCode);
    } catch (Exception e) {
        _log.warn(languageId + " is not a valid language id");
    }

    if (locale == null) {
        locale = Locale.getDefault();
    }

    return locale;
}

From source file:py.una.pol.karaku.test.test.ValidationMessagesTest.java

@BeforeClass
public static void before() {

    Locale locale = new Locale("es", "PY");
    ResourceBundle toAdd = ResourceBundle.getBundle("language.validation.karaku", locale);
    Enumeration<String> en = toAdd.getKeys();
    keys = new HashSet<String>();
    while (en.hasMoreElements()) {

        keys.add("{" + en.nextElement() + "}");

    }//w ww.j  a v  a  2s . com

}

From source file:I18nUtils.java

/**
 * Convert a string based locale into a Locale Object.
 * Assumes the string has form "{language}_{country}_{variant}".
 * Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr_MAC"
 *  //from   ww w . ja  va  2s  .c o  m
 * @param localeString The String
 * @return the Locale
 */
public static Locale getLocaleFromString(String localeString) {
    if (localeString == null) {
        return null;
    }
    localeString = localeString.trim();
    if (localeString.toLowerCase().equals("default")) {
        return Locale.getDefault();
    }

    // Extract language
    int languageIndex = localeString.indexOf('_');
    String language = null;
    if (languageIndex == -1) {
        // No further "_" so is "{language}" only
        return new Locale(localeString, "");
    } else {
        language = localeString.substring(0, languageIndex);
    }

    // Extract country
    int countryIndex = localeString.indexOf('_', languageIndex + 1);
    String country = null;
    if (countryIndex == -1) {
        // No further "_" so is "{language}_{country}"
        country = localeString.substring(languageIndex + 1);
        return new Locale(language, country);
    } else {
        // Assume all remaining is the variant so is "{language}_{country}_{variant}"
        country = localeString.substring(languageIndex + 1, countryIndex);
        String variant = localeString.substring(countryIndex + 1);
        return new Locale(language, country, variant);
    }
}

From source file:Main.java

/**
 * //ww  w  . ja v a 2 s  .  co  m
 * Returns the "parent" locale of a given locale.
 * 
 * 
 * If the original locale is only language-based, the {@link #NULL_LOCALE}
 * object is returned.
 * 
 * 
 * If the original locale is {@link #NULL_LOCALE}, then <code>null</code>
 * is returned.
 * 
 *
 * @param locale The original locale.
 * @return The parent locale.
 */
public static Locale getParentLocale(Locale locale) {
    Locale retValue = null;
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    if (!"".equals(variant)) {
        retValue = new Locale(language, country);
    } else if (!"".equals(country)) {
        retValue = new Locale(language);
    } else if (!"".equals(language)) {
        retValue = NULL_LOCALE;
    }

    return retValue;
}

From source file:DateFormatSymbolsDemo.java

static public void changeWeekDays() {

    Date today;//from   ww  w .  ja v a2  s  .  c  om
    String result;
    SimpleDateFormat formatter;
    DateFormatSymbols symbols;
    String[] defaultDays;
    String[] modifiedDays;

    symbols = new DateFormatSymbols(new Locale("en", "US"));
    defaultDays = symbols.getShortWeekdays();

    for (int i = 0; i < defaultDays.length; i++) {
        System.out.print(defaultDays[i] + "  ");
    }
    System.out.println();

    String[] capitalDays = { "", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
    symbols.setShortWeekdays(capitalDays);

    modifiedDays = symbols.getShortWeekdays();
    for (int i = 0; i < modifiedDays.length; i++) {
        System.out.print(modifiedDays[i] + "  ");
    }

    System.out.println();
    System.out.println();

    formatter = new SimpleDateFormat("E", symbols);
    today = new Date();
    result = formatter.format(today);
    System.out.println(result);
}

From source file:com.rcs.shoe.shop.fx.model.LanguageModel.java

public void setBundle(Language lang) {
    if (lang == null || lang.equals(this.bundle)) {
        return;/*from   w  ww. j  a  v a  2s  .co m*/
    }
    setLanguage(lang);
    bundle = ResourceBundle.getBundle("lang", new Locale(lang.getValue(), lang.toString()));
    setChanged();
    notifyObservers();
}

From source file:coreexample.BaseAppConfig.java

@Bean
public Locale systemLocale() {
    return new Locale("en", "US");
}

From source file:ListOfCitys.CityList.java

private static String getCountryName(String zone) throws IOException {
    String[] arry = zone.split(";"); //EU;DE
    zone = arry[1];/*from   w w  w .  j  a va2 s.  com*/
    String[] locales = Locale.getISOCountries();
    String countryName = "";
    JSONArray countryWithCities = new JSONArray();
    Map<String, List<String>> countryWithCityNames = new HashMap<>();
    for (String countryCode : locales) {
        Locale obj = new Locale("", countryCode);
        System.out
                .println("Country Code = " + obj.getCountry() + ", Country Name = " + obj.getDisplayCountry());
        countryWithCities.add(getCityNameForCountry(obj.getCountry(), countryCode));
        if (obj.getCountry().equalsIgnoreCase(zone)) {
            countryName = obj.getDisplayCountry();
            break;
        }
    }
    System.out.println("-----json array --- " + countryWithCities.toJSONString());
    try (FileWriter file = new FileWriter("test.json")) {
        file.write(countryWithCities.toJSONString());
    }
    return countryName;
}

From source file:io.leishvl.core.util.LocaleUtils.java

/**
 * Obtains an unmodifiable list of installed locales. This method is a wrapper around
 * {@link Locale#getAvailableLocales()}, which caches the locales and uses generics.
 * @return country names.//from w  w w  .j  a va 2  s.  c  o  m
 */
public static ImmutableList<Locale> availableLocaleList() {
    if (availableLocaleList == null) {
        synchronized (LocaleUtils.class) {
            if (availableLocaleList == null) {
                final ImmutableList.Builder<Locale> builder = new ImmutableList.Builder<>();
                final String[] locales = getISOCountries();
                for (final String countryCode : locales) {
                    builder.add(new Locale("", countryCode));
                }
                availableLocaleList = builder.build();
            }
        }
    }
    return availableLocaleList;
}

From source file:LocaleUtils.java

/**
 * Converts a String to a Locale. <p/> This method takes the string format of a locale and creates the
 * locale object from it. <p/>/*from w  w w .  jav  a  2 s  . c  o  m*/
 * <pre>
 *   LocaleUtils.toLocale("en")         = new Locale("en", "")
 *   LocaleUtils.toLocale("en_GB")      = new Locale("en", "GB")
 *   LocaleUtils.toLocale("en_GB_xxx")  = new Locale("en", "GB", "xxx")   (#)
 * </pre>
 * <p/> (#) The behaviour of the JDK variant constructor changed between JDK1.3 and JDK1.4. In JDK1.3, the
 * constructor upper cases the variant, in JDK1.4, it doesn't. Thus, the result from getVariant() may vary depending
 * on your JDK. <p/> This method validates the input strictly. The language code must be lowercase. The
 * country code must be uppercase. The separator must be an underscore. The length must be correct. 
 *
 * @param input the locale String to convert, null returns null
 * @return a Locale, null if null input
 * @throws IllegalArgumentException if the string is an invalid format
 */
public static Locale toLocale(String input) {
    if (input == null)
        return null;

    int len = input.length();
    if (len != 2 && len != 5 && len < 7)
        fail(input);

    char ch0 = input.charAt(0);
    char ch1 = input.charAt(1);

    if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z')
        fail(input);

    if (len == 2)
        return new Locale(input, "");

    if (input.charAt(2) != '_')
        fail(input);

    char ch3 = input.charAt(3);
    if (ch3 == '_')
        return new Locale(input.substring(0, 2), "", input.substring(4));

    char ch4 = input.charAt(4);
    if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z')
        fail(input);

    if (len == 5)
        return new Locale(input.substring(0, 2), input.substring(3, 5));

    if (input.charAt(5) != '_')
        fail(input);

    return new Locale(input.substring(0, 2), input.substring(3, 5), input.substring(6));
}