Example usage for java.util Locale getVariant

List of usage examples for java.util Locale getVariant

Introduction

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

Prototype

public String getVariant() 

Source Link

Document

Returns the variant code for this locale.

Usage

From source file:Main.java

public static void main(String[] args) {
    Locale locale = new Locale("en", "US", "WIN");

    System.out.println("Locale:" + locale);

    // get variant and print it
    System.out.println("Language:" + locale.getVariant());
}

From source file:org.xwiki.localization.LocaleUtils.java

/**
 * @param locale the locale/*from www .j ava  2s  .  co  m*/
 * @return the parent locale
 */
public static Locale getParentLocale(Locale locale) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();

    if (StringUtils.isEmpty(language)) {
        return null;
    }

    if (StringUtils.isEmpty(country)) {
        return Locale.ROOT;
    }

    if (StringUtils.isEmpty(variant)) {
        return new Locale(language);
    }

    return new Locale(language, country);
}

From source file:Main.java

/**
 * Test for lax Locale equality. More precisely, returns true if
 * (a) both are equal; (b) general only specifies language, and
 * specific has the same language; (c) general specifies language and
 * country, and specific has the same language and country. Else returns false.
 *///w w w .  j  a  va 2s  .  c om
public static boolean subsumes(Locale general, Locale specific) {
    if (general == null || specific == null)
        return false;
    if (general.equals(specific))
        return true;
    else if (general.getVariant().equals("")) {
        if (general.getCountry().equals("")) {
            if (general.getLanguage().equals(specific.getLanguage()))
                return true;
        } else {
            if (general.getLanguage().equals(specific.getLanguage())
                    && general.getCountry().equals(specific.getCountry()))
                return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * /*from   w w w  .j  a  va 2  s .  c  om*/
 * 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:Main.java

/**
 * <p>Obtains the list of locales to search through when performing
 * a locale search.</p>/*from   w  w w. j  a  va  2 s. c  om*/
 *
 * <pre>
 * localeLookupList(Locale("fr", "CA", "xxx"), Locale("en"))
 *   = [Locale("fr","CA","xxx"), Locale("fr","CA"), Locale("fr"), Locale("en"]
 * </pre>
 *
 * <p>The result list begins with the most specific locale, then the
 * next more general and so on, finishing with the default locale.
 * The list will never contain the same locale twice.</p>
 *
 * @param locale  the locale to start from, null returns empty list
 * @param defaultLocale  the default locale to use if no other is found
 * @return the unmodifiable list of Locale objects, 0 being locale, not null
 */
public static List<Locale> localeLookupList(Locale locale, Locale defaultLocale) {
    List<Locale> list = new ArrayList<Locale>(4);
    if (locale != null) {
        list.add(locale);
        if (locale.getVariant().length() > 0) {
            list.add(new Locale(locale.getLanguage(), locale.getCountry()));
        }
        if (locale.getCountry().length() > 0) {
            list.add(new Locale(locale.getLanguage(), ""));
        }
        if (!list.contains(defaultLocale)) {
            list.add(defaultLocale);
        }
    }
    return Collections.unmodifiableList(list);
}

From source file:org.craftercms.engine.util.LocaleUtils.java

/**
 * Returns a new url base on the given locale. The implementation returns an url with (delim is normally '_'):
 * <pre>//from w ww.  j  a  v  a2s . com
 *     baseName + delim + language + delim + country + delim + variant
 * </pre>
 */
public static String getLocalizedUrl(ContentBundleUrlParser urlParser, String baseUrl, Locale locale,
        String delim) {
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();

    if (StringUtils.isEmpty(language) && StringUtils.isEmpty(country) && StringUtils.isEmpty(variant)) {
        return baseUrl;
    }

    ContentBundleUrl parsedUrl = urlParser.getContentBundleUrl(baseUrl);
    String localizedUrl = baseUrl;

    if (StringUtils.isNotEmpty(parsedUrl.getBaseNameAndExtensionToken())) {
        StringBuilder localizedUrlBuilder = new StringBuilder(parsedUrl.getBaseNameAndExtensionToken());

        localizedUrlBuilder.append(delim);
        if (StringUtils.isNotEmpty(variant)) {
            localizedUrlBuilder.append(language).append(delim).append(country).append(delim).append(variant);
        } else if (StringUtils.isNotEmpty(country)) {
            localizedUrlBuilder.append(language).append(delim).append(country);
        } else {
            localizedUrlBuilder.append(language);
        }

        localizedUrlBuilder.insert(0, parsedUrl.getPrefix()).append(parsedUrl.getSuffix());

        localizedUrl = localizedUrlBuilder.toString();
    }

    return localizedUrl;
}

From source file:org.atomserver.utils.locale.LocaleUtils.java

public static Locale toLocale(String str) {
    Locale locale = org.apache.commons.lang.LocaleUtils.toLocale(str);
    if (!org.apache.commons.lang.LocaleUtils.isAvailableLocale(locale))
        return null;

    // We do NOT allow variants
    if (!locale.getVariant().equals(""))
        return null;

    return locale;
}

From source file:no.kantega.commons.util.LocaleLabels.java

public static Enumeration getKeys(String bundleName, Locale locale) {
    String loc = locale.getLanguage() + "_" + locale.getCountry();
    if (isNotBlank(locale.getVariant())) {
        loc += "_" + locale.getVariant();
    }//from  w  w w .  j  a  va 2s  .c om

    PropertyResourceBundle bundle = getBundle(bundleName, loc);
    if (bundle == null) {
        return null;
    }
    return bundle.getKeys();
}

From source file:Main.java

public static String toLanguageTag(Locale locale) {
    StringBuilder languageTag = new StringBuilder();
    languageTag.append(locale.getLanguage());
    if (!isNullOrEmpty(locale.getCountry())) {
        languageTag.append('-').append(locale.getCountry());
    }/*from  w  w  w  .j a  va 2 s  . c o  m*/
    if (!isNullOrEmpty(locale.getVariant())) {
        languageTag.append('-').append(locale.getVariant());
    }
    return languageTag.toString();
}

From source file:org.ebayopensource.turmeric.eclipse.errorlibrary.properties.ui.wizards.NewPropertiesContentErrorLibraryWizardPage.java

private static String toString(Locale locale) {
    final String language = locale.getLanguage();
    final String country = locale.getCountry();
    final String variant = locale.getVariant();
    boolean l = language.length() != 0;
    boolean c = country.length() != 0;
    boolean v = variant.length() != 0;
    StringBuffer result = new StringBuffer(language);
    /*/*w ww. j  a  v  a  2  s .com*/
     * ibm@118171 start The Serbian locale does not obey the convention of
     * language_country_variant, so we have to special-case it here pending
     * a properly thought-out fix
     */
    if ((language.equals("sh") || language.equals("sr")) && (country.equals("RS"))
            && (variant.equals("Latn") || variant.equals("Cyrl"))) {
        return (language + "-" + variant + "-" + country);
    }
    /* ibm@118171 end */

    if (c || (l && v)) {
        result.append('-').append(country); // This may just append '-'
    }
    if (v && (l || c)) {
        result.append('-').append(variant);
    }
    return result.toString();
}