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.b3log.latke.plugin.AbstractPlugin.java

/**
 * Processes languages. Retrieves language labels with default locale, then sets them into the specified data model.
 * /*from www  .  jav a 2  s  .  c o m*/
 * @param dataModel the specified data model
 */
private void handleLangs(final Map<String, Object> dataModel) {
    final Locale locale = Latkes.getLocale();
    final String language = locale.getLanguage();
    final String country = locale.getCountry();
    final String variant = locale.getVariant();

    final StringBuilder keyBuilder = new StringBuilder(language);

    if (!Strings.isEmptyOrNull(country)) {
        keyBuilder.append("_").append(country);
    }
    if (!Strings.isEmptyOrNull(variant)) {
        keyBuilder.append("_").append(variant);
    }

    final String localKey = keyBuilder.toString();
    final Properties props = langs.get(localKey);

    if (null == props) {
        return;
    }

    final Set<Object> keySet = props.keySet();

    for (final Object key : keySet) {
        dataModel.put((String) key, props.getProperty((String) key));
    }
}

From source file:org.broadleafcommerce.common.i18n.service.TranslationServiceImpl.java

@Override
public String getTranslatedValue(Object entity, String property, Locale locale) {
    TranslatedEntity entityType = getEntityType(entity);
    String entityId = dao.getEntityId(entityType, entity);

    String localeCode = locale.getLanguage();
    String localeCountryCode = localeCode;
    if (StringUtils.isNotBlank(locale.getCountry())) {
        localeCountryCode += "_" + locale.getCountry();
    }//from w w w  .j a v a  2s. co  m
    boolean isValidForCache = false;
    if (extensionManager != null) {
        ExtensionResultHolder<Boolean> response = new ExtensionResultHolder<Boolean>();
        response.setResult(false);
        extensionManager.getProxy().isValidState(response);
        isValidForCache = response.getResult();
    }
    if (!BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox() || !isValidForCache) {
        Translation translation = dao.readTranslation(entityType, entityId, property, localeCode,
                localeCountryCode, ResultType.IGNORE);
        if (translation != null) {
            return translation.getTranslatedValue();
        } else {
            return null;
        }
    }

    return getOverrideTranslatedValue(property, entityType, entityId, localeCode, localeCountryCode);
}

From source file:org.openmrs.module.initializer.InitializerMessageSource.java

/**
 * @see AbstractMessageSource#resolveCode(String, Locale)
 *///from  ww  w . j  a  v  a2  s . c o  m
@Override
protected MessageFormat resolveCode(String code, Locale locale) {
    if (showMessageCode) {
        return new MessageFormat(code);
    }
    PresentationMessage pm = getPresentation(code, locale); // Check exact match
    if (pm == null) {
        if (locale.getVariant() != null) {
            pm = getPresentation(code, new Locale(locale.getLanguage(), locale.getCountry())); // Try to match
                                                                                               // language and
                                                                                               // country
            if (pm == null) {
                pm = getPresentation(code, new Locale(locale.getLanguage())); // Try to match language only
            }
        }
    }
    if (pm != null) {
        return new MessageFormat(pm.getMessage());
    }
    return null;
}

From source file:org.azyva.dragom.cliutil.CliUtil.java

/**
 * Returns the version of a text resource appropriate for the current default
 * Locale./*from  ww  w.j  a  va2 s  .c  o m*/
 *
 * <p>A Reader is returned so that character encoding is taken into consideration.
 * The text resource is assumed to be encoded with UTF-8, regardless of the
 * platform default encoding.
 *
 * <p>The algorithm used for selecting the appropriate resource is similar to the
 * one implemented by ResourceBundle.getBundle.
        
 * <p>The resource base name is split on the last ".", if any, and the candidate
 * variants are inserted before it.
 *
 * @param clazz Class to which the resource belongs.
 * @param resourceBaseName Base name of the resource.
 * @return Resource as an InputStream, just as Class.getResourceAsStream would
 *   return. null if no resource version exists.
 */
public static Reader getLocalizedTextResourceReader(Class<?> clazz, String resourceBaseName) {
    int indexDot;
    String resourceBaseNamePrefix;
    String resourceBaseNameSuffix;
    Locale locale;
    String[] arrayCandidate;

    indexDot = resourceBaseName.lastIndexOf('.');

    if (indexDot != -1) {
        resourceBaseNamePrefix = resourceBaseName.substring(0, indexDot);
        resourceBaseNameSuffix = resourceBaseName.substring(indexDot);
    } else {
        resourceBaseNamePrefix = resourceBaseName;
        resourceBaseNameSuffix = "";
    }

    locale = Locale.getDefault();

    arrayCandidate = new String[7];

    arrayCandidate[0] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getScript() + "_"
            + locale.getCountry() + "_" + locale.getVariant();
    arrayCandidate[1] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getScript() + "_"
            + locale.getCountry();
    arrayCandidate[2] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getScript();
    arrayCandidate[3] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getCountry() + "_"
            + locale.getVariant();
    arrayCandidate[4] = resourceBaseNamePrefix + "_" + locale.getLanguage() + "_" + locale.getCountry();
    arrayCandidate[5] = resourceBaseNamePrefix + "_" + locale.getLanguage();
    arrayCandidate[6] = resourceBaseNamePrefix;

    for (String candidate : arrayCandidate) {
        if (!candidate.endsWith("_")) {
            InputStream inputStreamResource;

            inputStreamResource = clazz.getResourceAsStream(candidate + resourceBaseNameSuffix);

            if (inputStreamResource != null) {
                try {
                    return new InputStreamReader(inputStreamResource, "UTF-8");
                } catch (UnsupportedEncodingException uee) {
                    throw new RuntimeException(uee);
                }
            }
        }
    }

    return null;
}

From source file:com.github.mrstampy.gameboot.usersession.processor.UserMessageProcessorTest.java

private User countryCodeTest(User user, UserMessage m) throws Exception {
    assertNotEquals(COUNTRY_CODE, user.getCountryCode());
    m.setCountryCode(COUNTRY_CODE);/* w ww .j a v  a 2  s  .c  om*/
    m.setSystemId(SYSTEM_ID); // set by the system

    user = updateCheck(processor.process(m));
    assertEquals(COUNTRY_CODE, user.getCountryCode());
    assertEquals(1, registry.size());

    Locale locale = registry.get(SYSTEM_ID);
    assertEquals(LANGUAGE_CODE, locale.getLanguage());
    assertEquals(COUNTRY_CODE, locale.getCountry());

    m.setCountryCode(null);
    return user;
}

From source file:org.springframework.integration.expression.ReloadableResourceBundleExpressionSource.java

/**
 * Calculate the filenames for the given bundle basename and Locale,
 * appending language code, country code, and variant code.
 * E.g.: basename "expressions", Locale "de_AT_oo" -> "expressions_de_AT_OO",
 * "expressions_de_AT", "expressions_de".
 * <p>Follows the rules defined by {@link java.util.Locale#toString()}.
 * @param basename the basename of the bundle
 * @param locale the locale//from  w  w w .j  av  a  2s .co  m
 * @return the List of filenames to check
 */
private List<String> calculateFilenamesForLocale(String basename, Locale locale) {
    List<String> result = new ArrayList<String>(3);
    String language = locale.getLanguage();
    String country = locale.getCountry();
    String variant = locale.getVariant();
    StringBuilder temp = new StringBuilder(basename);

    temp.append('_');
    if (language.length() > 0) {
        temp.append(language);
        result.add(0, temp.toString());
    }

    temp.append('_');
    if (country.length() > 0) {
        temp.append(country);
        result.add(0, temp.toString());
    }

    if (variant.length() > 0 && (language.length() > 0 || country.length() > 0)) {
        temp.append('_').append(variant);
        result.add(0, temp.toString());
    }

    return result;
}

From source file:eu.ggnet.dwoss.customer.priv.OldCustomer.java

public void setPayCountry(Locale country) {
    if (country == null)
        throw new NullPointerException("Null not allowed");
    else/*w w  w  .j  ava  2  s  .  c  om*/
        payIsoCountry = country.getCountry();
}

From source file:eu.ggnet.dwoss.customer.priv.OldCustomer.java

public void setShipCountry(Locale country) {
    if (country == null)
        throw new NullPointerException("Null not allowed");
    else//from  w  ww .  ja  v  a  2 s.  c  om
        shipIsoCountry = country.getCountry();
}

From source file:org.gnucash.android.ui.account.AccountsActivity.java

/**
 * Loads default setting for currency and performs app first-run initialization
 */// www.  j a  v  a2 s .  c om
private void init() {
    PreferenceManager.setDefaultValues(this, R.xml.fragment_transaction_preferences, false);

    Locale locale = Locale.getDefault();
    //sometimes the locale en_UK is returned which causes a crash with Currency
    if (locale.getCountry().equals("UK")) {
        locale = new Locale(locale.getLanguage(), "GB");
    }

    String currencyCode;
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    try { //there are some strange locales out there
        currencyCode = prefs.getString(getString(R.string.key_default_currency),
                Currency.getInstance(locale).getCurrencyCode());
    } catch (Exception e) {
        Log.e(LOG_TAG, e.getMessage());
        currencyCode = "USD";
    }

    Money.DEFAULT_CURRENCY_CODE = currencyCode;

    boolean firstRun = prefs.getBoolean(getString(R.string.key_first_run), true);
    if (firstRun) {
        createDefaultAccounts();
        //default to using double entry and save the preference explicitly
        prefs.edit().putBoolean(getString(R.string.key_use_double_entry), true).commit();
    }

    if (hasNewFeatures()) {
        showWhatsNewDialog(this);
    }

}

From source file:it.cnr.icar.eric.client.ui.thin.UserPreferencesBean.java

/** Sets the locales to 1st supported locale (directly or indirectly). */
private void initLocales() {
    Iterator<?> requestLocales = FacesContext.getCurrentInstance().getExternalContext().getRequestLocales();
    Collection<Locale> supportedLocales = getSupportedUiLocales();
    //iterate though client preferred locales until find supported 
    while (requestLocales.hasNext()) {
        Locale loc = (Locale) requestLocales.next();
        // try direct match
        if (supportedLocales.contains(loc)) {
            init(loc);/*from w  w w  .java  2 s  . co m*/
            return;
        }
        // try to use language country, without variant
        if (loc.getVariant() != null && !"".equals(loc.getVariant())) {
            loc = new Locale(loc.getLanguage(), loc.getCountry());
            if (supportedLocales.contains(loc)) {
                init(loc);
                return;
            }
        }
        // try to use language without country and variant
        if (loc.getCountry() != null && !"".equals(loc.getCountry())) {
            loc = new Locale(loc.getLanguage());
            if (supportedLocales.contains(loc)) {
                init(loc);
                return;
            }
        }
    }
    // fall to default locale, from properties (or en_US, if not defined)
    init(getDefaultLocale());
}