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:com.puppycrawl.tools.checkstyle.CheckerTest.java

@Test
public void testCacheFileChangeInConfig() throws Exception {
    final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);

    final DefaultConfiguration treeWalkerConfig = createCheckConfig(TreeWalker.class);
    treeWalkerConfig.addChild(checkConfig);

    final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
    checkerConfig.addAttribute("charset", "UTF-8");
    checkerConfig.addChild(treeWalkerConfig);
    checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath());

    final Checker checker = new Checker();
    final Locale locale = Locale.ROOT;
    checker.setLocaleCountry(locale.getCountry());
    checker.setLocaleLanguage(locale.getLanguage());
    checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    checker.configure(checkerConfig);/*from  w w w .j a va2 s .  c o m*/
    checker.addListener(new BriefUtLogger(stream));

    final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath();
    final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;

    verify(checker, pathToEmptyFile, pathToEmptyFile, expected);

    // update Checker config
    checker.destroy();
    checker.configure(checkerConfig);

    final Checker otherChecker = new Checker();
    otherChecker.setLocaleCountry(locale.getCountry());
    otherChecker.setLocaleLanguage(locale.getLanguage());
    otherChecker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
    otherChecker.configure(checkerConfig);
    otherChecker.addListener(new BriefUtLogger(stream));
    // here is diff with previous checker
    checkerConfig.addAttribute("fileExtensions", "java,javax");

    // one more time on updated config
    verify(otherChecker, pathToEmptyFile, pathToEmptyFile, expected);
}

From source file:org.primeframework.mvc.parameter.convert.converters.LocaleConverterTest.java

@Test
public void fromStrings() {
    GlobalConverter converter = new LocaleConverter();
    Locale locale = (Locale) converter.convertFromStrings(Locale.class, null, "testExpr",
            ArrayUtils.toArray((String) null));
    assertNull(locale);//  w  w w.j a v  a 2  s  .  c  o m

    locale = (Locale) converter.convertFromStrings(Locale.class, null, "testExpr", ArrayUtils.toArray("en"));
    assertEquals(locale.getLanguage(), "en");

    locale = (Locale) converter.convertFromStrings(Locale.class, null, "testExpr", ArrayUtils.toArray("en_US"));
    assertEquals(locale.getLanguage(), "en");
    assertEquals(locale.getCountry(), "US");

    locale = (Locale) converter.convertFromStrings(Locale.class, null, "testExpr",
            ArrayUtils.toArray("en", "US"));
    assertEquals(locale.getLanguage(), "en");
    assertEquals(locale.getCountry(), "US");

    locale = (Locale) converter.convertFromStrings(Locale.class, null, "testExpr",
            ArrayUtils.toArray("en_US_UTF8"));
    assertEquals(locale.getLanguage(), "en");
    assertEquals(locale.getCountry(), "US");
    assertEquals(locale.getVariant(), "UTF8");

    locale = (Locale) converter.convertFromStrings(Locale.class, null, "testExpr",
            ArrayUtils.toArray("en", "US", "UTF8"));
    assertEquals(locale.getLanguage(), "en");
    assertEquals(locale.getCountry(), "US");
    assertEquals(locale.getVariant(), "UTF8");
}

From source file:org.atomserver.core.EntryMetaData.java

private void decodeLocale(Locale localeObj) {
    if (localeObj == null) {
        language = country = "**";
    } else {/*from   w  ww  . ja  v a  2s  .  c om*/
        language = localeObj.getLanguage();
        if (language.equals("")) {
            String msg = "Unknown Locale:: " + localeObj;
            log.error(msg);
            throw new AtomServerException(msg);
        }
        country = (localeObj.getCountry().equals("")) ? "**" : localeObj.getCountry();
    }
}

From source file:com.bdaum.zoom.gps.naming.geonaming.internal.GeoNamesService.java

public WaypointArea[] findLocation(String address)
        throws IOException, WebServiceException, SAXException, ParserConfigurationException, HttpException {
    List<WaypointArea> pnts = new ArrayList<WaypointArea>();
    Locale locale = Locale.getDefault();
    String template = "http://api.geonames.org/search?" + getSearchParms() //$NON-NLS-1$
            + "&countryBias={1}&lang={2}&maxRows={3}"; //$NON-NLS-1$
    String query = NLS.bind(template, new Object[] { URLEncoder.encode(address, "UTF-8"), locale.getCountry(), //$NON-NLS-1$
            locale.getLanguage(), GeoCodeParser.MAXRESULTS });
    try (InputStream in = openGeonamesService(query)) {
        new GeoCodeParser(in).parse(pnts);
        return pnts.toArray(new WaypointArea[pnts.size()]);
    }/*from   w ww  .j  a  v  a  2s.co  m*/
}

From source file:org.openmrs.module.custommessage.CustomMessageSource.java

/**
 * @see AbstractMessageSource#resolveCode(String, Locale)
 *//*from   w w w  . j  a v  a2s.com*/
@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:LocaleMap.java

/**
 * Gets an object based on the given locale. An automatic fallback mechanism is used:
 * if nothing is found for language-COUNTRY-variant, then language-COUNTRY is searched,
 * the language, and finally "" (empty string). If nothing is found null is returned.
 *//*from ww  w .  j  a v a 2s.  c o  m*/
public Object get(Locale locale) {
    if (map.size() == 0)
        return null;

    String full = getFullKey(locale);

    if (!searchMap.containsKey(full)) {
        if (map.containsKey(full)) {
            Object object = map.get(full);
            searchMap.put(full, object);
            return object;
        }

        String altKey = locale.getLanguage() + '-' + locale.getCountry();
        Object object = map.get(altKey);
        if (object != null) {
            searchMap.put(full, object);
            return object;
        }

        altKey = locale.getLanguage();
        object = map.get(altKey);
        if (object != null) {
            searchMap.put(full, object);
            return object;
        }

        object = map.get("");
        if (object != null) {
            searchMap.put(full, object);
            return object;
        }

        searchMap.put(full, NO_RESULT);
    }

    Object result = searchMap.get(full);
    return result == NO_RESULT ? null : result;
}

From source file:org.jahia.bin.Logout.java

protected String resolveLanguage(HttpServletRequest request, final JCRSiteNode site) throws JahiaException {
    final List<Locale> newLocaleList = new ArrayList<Locale>();
    List<Locale> siteLanguages = Collections.emptyList();
    try {//from   ww w  .  ja v  a 2  s.  c  om
        if (site != null) {
            siteLanguages = site.getLanguagesAsLocales();
        }
    } catch (Exception t) {
        logger.debug("Exception while getting language settings as locales", t);
    }

    // retrieve the browser locales
    for (@SuppressWarnings("unchecked")
    Iterator<Locale> browserLocales = new EnumerationIterator(request.getLocales()); browserLocales
            .hasNext();) {
        final Locale curLocale = browserLocales.next();
        if (siteLanguages.contains(curLocale)) {
            addLocale(site, newLocaleList, curLocale);
        } else if (!StringUtils.isEmpty(curLocale.getCountry())) {
            final Locale langOnlyLocale = new Locale(curLocale.getLanguage());
            if (siteLanguages.contains(langOnlyLocale)) {
                addLocale(site, newLocaleList, langOnlyLocale);
            }
        }
    }

    String language = DEFAULT_LOCALE;
    if (!newLocaleList.isEmpty()) {
        language = newLocaleList.get(0).toString();
    } else if (site != null) {
        language = site.getDefaultLanguage();
    } else if (!StringUtils.isEmpty(SettingsBean.getInstance().getDefaultLanguageCode())) {
        language = SettingsBean.getInstance().getDefaultLanguageCode();
    }
    return language;
}

From source file:com.github.mrstampy.gameboot.locale.processor.LocaleProcessorTest.java

private void testCurrent(Locale expected) throws Exception {
    CurrentLocaleMessage cur = new CurrentLocaleMessage();
    cur.setSystemId(SYSTEM_ID_KEY);//from  w  ww . j  a  v a  2s . c  om

    Response r = current.process(cur);

    assertEquals(ResponseCode.SUCCESS, r.getResponseCode());
    assertNotNull(r.getPayload());
    assertEquals(1, r.getPayload().length);
    assertTrue(r.getPayload()[0] instanceof LocaleBean);

    LocaleBean lb = (LocaleBean) r.getPayload()[0];
    assertEquals(expected.getCountry(), lb.getCountryCode());
    assertEquals(expected.getLanguage(), lb.getLanguageCode());
}

From source file:org.apache.myfaces.application.jsp.JspViewHandlerImpl.java

public Locale calculateLocale(FacesContext facesContext) {
    Iterator locales = facesContext.getExternalContext().getRequestLocales();
    while (locales.hasNext()) {
        Locale locale = (Locale) locales.next();
        for (Iterator it = facesContext.getApplication().getSupportedLocales(); it.hasNext();) {
            Locale supportLocale = (Locale) it.next();
            // higher priority to a language match over an exact match
            // that occures further down (see Jstl Reference 1.0 8.3.1)
            if (locale.getLanguage().equals(supportLocale.getLanguage())
                    && (supportLocale.getCountry() == null || supportLocale.getCountry().length() == 0)) {
                return supportLocale;
            } else if (supportLocale.equals(locale)) {
                return supportLocale;
            }/*from   w w  w .  j a va  2  s.  c  om*/
        }
    }

    Locale defaultLocale = facesContext.getApplication().getDefaultLocale();
    return defaultLocale != null ? defaultLocale : Locale.getDefault();
}

From source file:com.m2a.struts.M2AFormBase.java

public String getLocaleCountry() {
    Locale locale = getSessionLocale();
    if (null == locale) {
        locale = Locale.getDefault();
    }//from   ww w.  ja  va2s  .  c om
    return locale.getCountry();
}