Example usage for java.util Locale getCountry

List of usage examples for java.util Locale getCountry

Introduction

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

Prototype

public String getCountry() 

Source Link

Document

Returns the country/region code for this locale, which should either be the empty string, an uppercase ISO 3166 2-letter code, or a UN M.49 3-digit code.

Usage

From source file:org.openmrs.ConceptNameTag.java

/**
 * A factory method which generates a preferred country tag from the country-code portion of a
 * locale./*from w  ww.j  a v a  2s . c om*/
 * 
 * @param locale locale from which the country-code will be used
 * @return concept-name-tag for country, or null if country component wasn't specified in locale
 */
public static ConceptNameTag preferredCountryTagFor(Locale locale) {
    ConceptNameTag preferredCountryTag = null;
    String country = locale.getCountry();
    if (StringUtils.isNotEmpty(country)) {
        preferredCountryTag = preferredCountryTagFor(locale.getCountry());
    }
    return preferredCountryTag;
}

From source file:nl.strohalm.cyclos.utils.PropertiesHelper.java

/**
 * Reads a resource bundle from a properties file encoded as utf-8
 *///w  w  w . j av a 2  s  .co m
public static ResourceBundle readBundle(final String baseName, Locale locale) {
    if (locale == null) {
        locale = Locale.getDefault();
    }

    final List<String> suffixes = new ArrayList<String>();

    if (locale != null) {
        suffixes.add("");
        if (StringUtils.isNotEmpty(locale.getLanguage())) {
            suffixes.add("_" + locale.getLanguage());
        }
        if (StringUtils.isNotEmpty(locale.getCountry()) && StringUtils.isNotEmpty(locale.getLanguage())) {
            suffixes.add("_" + locale.getLanguage() + "_" + locale.getCountry());
        }
        if (StringUtils.isNotEmpty(locale.getVariant()) && StringUtils.isNotEmpty(locale.getCountry())
                && StringUtils.isNotEmpty(locale.getLanguage())) {
            suffixes.add("_" + locale.getLanguage() + "_" + locale.getCountry() + "_" + locale.getVariant());
        }
    }

    PropertiesResourceBundle bundle = null;
    for (final String suffix : suffixes) {
        final Properties properties = loadFromResource(baseName + suffix);
        if (properties != null) {
            final PropertiesResourceBundle current = new PropertiesResourceBundle(properties, bundle);
            bundle = current;
        }
    }
    if (bundle == null) {
        throw new IllegalArgumentException(
                "Error loading properties resource bundle for baseName=" + baseName + " and locale=" + locale);
    }
    return bundle;
}

From source file:com.android.providers.contacts.LocaleSet.java

/**
 * Modified from://  w w  w.  j  av  a 2 s  .  co m
 * https://github.com/apache/cordova-plugin-globalization/blob/master/src/android/Globalization.java
 *
 * Returns a well-formed ITEF BCP 47 language tag representing this locale string
 * identifier for the client's current locale
 *
 * @return String: The BCP 47 language tag for the current locale
 */
private static String toBcp47Language(Locale loc) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return loc.toLanguageTag();
    }

    // we will use a dash as per BCP 47
    final char SEP = '-';
    String language = loc.getLanguage();
    String region = loc.getCountry();
    String variant = loc.getVariant();

    // special case for Norwegian Nynorsk since "NY" cannot be a variant as per BCP 47
    // this goes before the string matching since "NY" wont pass the variant checks
    if (language.equals("no") && region.equals("NO") && variant.equals("NY")) {
        language = "nn";
        region = "NO";
        variant = "";
    }

    if (language.isEmpty() || !language.matches("\\p{Alpha}{2,8}")) {
        language = "und"; // Follow the Locale#toLanguageTag() implementation
        // which says to return "und" for Undetermined
    } else if (language.equals("iw")) {
        language = "he"; // correct deprecated "Hebrew"
    } else if (language.equals("in")) {
        language = "id"; // correct deprecated "Indonesian"
    } else if (language.equals("ji")) {
        language = "yi"; // correct deprecated "Yiddish"
    }

    // ensure valid country code, if not well formed, it's omitted
    if (!region.matches("\\p{Alpha}{2}|\\p{Digit}{3}")) {
        region = "";
    }

    // variant subtags that begin with a letter must be at least 5 characters long
    if (!variant.matches("\\p{Alnum}{5,8}|\\p{Digit}\\p{Alnum}{3}")) {
        variant = "";
    }

    StringBuilder bcp47Tag = new StringBuilder(language);
    if (!region.isEmpty()) {
        bcp47Tag.append(SEP).append(region);
    }
    if (!variant.isEmpty()) {
        bcp47Tag.append(SEP).append(variant);
    }

    return bcp47Tag.toString();
}

From source file:nl.knaw.dans.common.lang.ResourceLocator.java

/**
 * Get the URL for a locale-specific resource. The algorithm conforms to java.util.ResourceBundle
 * policy. That is for given path x and language-code nl and country-code NL, will look for
 * <ul>/*from ww w  .ja  v a 2s . c  om*/
 * <li>x_nl_NL</li>
 * <li>x_nl</li>
 * <li>x</li>
 * </ul>
 * in that order.
 * 
 * @param path
 *        "/"-separated relative path, without extension
 * @param locale
 *        Locale, may be <code>null</code>
 * @param extension
 *        extension (without "."), may be <code>null</code>
 * @return URL of the resource or <code>null</code>
 */
public static URL getURL(final String path, final Locale locale, final String extension) {
    URL url = null;

    final boolean language = locale != null && StringUtils.isNotBlank(locale.getLanguage());
    final boolean country = locale != null && StringUtils.isNotBlank(locale.getCountry());

    if (language && country) {
        // x_nl_NL.ext
        url = getURL(getFullPath(path, locale, extension));
    }

    if (url == null && language) {
        // x_nl.ext
        url = getURL(getLanguagePath(path, locale, extension));
    }

    if (url == null) {
        // x.ext
        url = getURL(getPath(path, extension));
    }

    return url;
}

From source file:net.sf.eclipsecs.core.builder.CheckerFactory.java

/**
 * Creates a new checker and configures it with the given configuration file.
 *
 * @param input/* w  w w  .  j a  v a2 s .  c  o m*/
 *            the input source for the configuration file
 * @param configFileUri
 *            the URI of the configuration file, or <code>null</code> if it could not be determined
 * @param propResolver
 *            a property resolver null
 * @param project
 *            the project
 * @return the newly created Checker
 * @throws CheckstyleException
 *             an exception during the creation of the checker occured
 */
private static Checker createCheckerInternal(InputSource input, PropertyResolver propResolver, IProject project)
        throws CheckstyleException, CheckstylePluginException {

    // load configuration
    Configuration configuration = ConfigurationLoader.loadConfiguration(input, propResolver, true);

    // create and configure checker
    Checker checker = new Checker();
    checker.setModuleClassLoader(CheckstylePlugin.getDefault().getAddonExtensionClassLoader());
    try {
        checker.setCharset(project.getDefaultCharset());
    } catch (UnsupportedEncodingException e) {
        CheckstylePluginException.rethrow(e);
    } catch (CoreException e) {
        CheckstylePluginException.rethrow(e);
    }

    // set the eclipse platform locale
    Locale platformLocale = CheckstylePlugin.getPlatformLocale();
    checker.setLocaleLanguage(platformLocale.getLanguage());
    checker.setLocaleCountry(platformLocale.getCountry());
    checker.setClassloader(sSharedClassLoader);

    checker.configure(configuration);

    // reset the basedir if it is set so it won't get into the plugins way
    // of determining workspace resources from checkstyle reported file
    // names, see
    // https://sourceforge.net/tracker/?func=detail&aid=2880044&group_id=80344&atid=559497
    if (checker.getBasedir() != null) {
        checker.setBasedir(null);
    }

    return checker;
}

From source file:net.geoprism.localization.LocaleManager.java

public static String getLocaleName(Locale _locale) {
    StringBuffer buffer = new StringBuffer();

    if (_locale.getLanguage() != null && _locale.getLanguage().length() > 0) {
        buffer.append(_locale.getLanguage());
    }//from ww w .jav a2  s.co  m

    if (_locale.getCountry() != null && _locale.getCountry().length() > 0) {
        buffer.append("-" + _locale.getCountry());
    }

    if (_locale.getVariant() != null && _locale.getVariant().length() > 0) {
        buffer.append("-" + _locale.getVariant());
    }

    return buffer.toString();
}

From source file:com.haulmont.cuba.core.entity.LocaleHelper.java

public static String getLocalizedName(String localeBundle) {
    Locale locale = AppBeans.get(UserSessionSource.class).getLocale();
    String localeName = null;//from   w ww  .ja v a 2  s  . co m
    if (StringUtils.isNotEmpty(localeBundle)) {
        Properties localeProperties = loadProperties(localeBundle);
        if (localeProperties != null) {
            String key = locale.getLanguage();
            if (StringUtils.isNotEmpty(locale.getCountry()))
                key += "_" + locale.getCountry();
            if (localeProperties.containsKey(key))
                localeName = (String) localeProperties.get(key);
        }
    }
    return localeName;
}

From source file:LocaleUtils.java

/**
 * <p>Obtains the list of locales to search through when performing
 * a locale search.</p>//w  w w  .j  av a 2s. c o m
 *
 * <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, never null
 */
public static List localeLookupList(Locale locale, Locale defaultLocale) {
    List list = new ArrayList(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) == false) {
            list.add(defaultLocale);
        }
    }
    return Collections.unmodifiableList(list);
}

From source file:com.code4bones.utils.HttpUtils.java

private static void addLocaleToHttpAcceptLanguage(StringBuilder builder, Locale locale) {
    String language = convertObsoleteLanguageCodeToNew(locale.getLanguage());
    if (language != null) {
        builder.append(language);/*from  www.  jav a  2  s  .c o m*/
        String country = locale.getCountry();
        if (country != null) {
            builder.append("-");
            builder.append(country);
        }
    }
}

From source file:org.languagetool.Languages.java

@Nullable
private static Language getLanguageForLanguageNameAndCountry(Locale locale) {
    for (Language language : LANGUAGES) {
        if (language.getShortName().equals(locale.getLanguage())) {
            final List<String> countryVariants = Arrays.asList(language.getCountries());
            if (countryVariants.contains(locale.getCountry())) {
                return language;
            }/*  www .j  a  v a2s  . c o m*/
        }
    }
    return null;
}