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:countries.ListCountry.java

public void getListOfCountries() {
    int supportedLocale = 0, nonSupportedLocale = 0;

    for (String countryCode : getISOCountries()) {

        Locale locale = null;
        if (!languagesOfCountries.containsKey(countryCode)) {
            locale = new Locale("", countryCode);
            nonSupportedLocale++;// w  w w  .j a  v  a2 s  .c  o  m
        } else {
            // create a Locale with own country's languages
            locale = new Locale(languagesOfCountries.get(countryCode), countryCode);
            supportedLocale++;
        }
        out.printf("Country Code: %1$2s, Name: %2$-45s %3$-45s %4$-45s Languages: %5$10s\n",
                locale.getCountry(), locale.getDisplayCountry(ENGLISH),
                parens(locale.getDisplayCountry(FRENCH)), parens(locale.getDisplayCountry(locale)),
                locale.getDisplayLanguage());
    }
    out.println("nonSupportedLocale: " + nonSupportedLocale);
    out.println("supportedLocale: " + supportedLocale);
}

From source file:de.alpharogroup.resourcebundle.properties.PropertiesExtensions.java

/**
 * Load the properties file from the given class object. The filename from the properties file
 * is the same as the simple name from the class object and it looks at the same path as the
 * given class object. If locale is not null than the language will be added to the filename
 * from the properties file.//from   w  ww . j ava2  s  . co  m
 *
 * @param clazz
 *            the clazz
 * @param locale
 *            the locale
 * @return the properties
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadPropertiesFromClassObject(final Class<?> clazz, final Locale locale)
        throws IOException {
    if (null == clazz) {
        throw new IllegalArgumentException("Class object must not be null!!!");
    }
    StringBuilder propertiesName = new StringBuilder();
    Properties properties = null;
    String language = null;
    String filename = null;
    String pathAndFilename = null;
    File propertiesFile = null;
    String absoluteFilename = null;
    final String packagePath = PackageExtensions.getPackagePathWithSlash(clazz);
    final List<String> missedFiles = new ArrayList<>();
    if (null != locale) {
        propertiesName.append(clazz.getSimpleName());
        language = locale.getLanguage();
        if ((null != language) && !language.isEmpty()) {
            propertiesName.append("_").append(language);
        }

        final String country = locale.getCountry();
        if ((null != country) && !country.isEmpty()) {
            propertiesName.append("_").append(country);
        }
        propertiesName.append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        URL url = ClassExtensions.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }

        if ((null != propertiesFile) && propertiesFile.exists()) {
            properties = PropertiesExtensions.loadProperties(pathAndFilename);
        } else {
            propertiesName = new StringBuilder();
            if (null != locale) {
                propertiesName.append(clazz.getSimpleName());
                language = locale.getLanguage();
                if ((null != language) && !language.isEmpty()) {
                    propertiesName.append("_").append(language);
                }
                propertiesName.append(FileExtension.PROPERTIES.getExtension());
                filename = propertiesName.toString().trim();
                pathAndFilename = packagePath + filename;
                url = ClassExtensions.getResource(clazz, filename);

                if (url != null) {
                    absoluteFilename = url.getFile();
                } else {
                    missedFiles.add("File with filename '" + filename + "' does not exists.");
                }
                if (null != absoluteFilename) {
                    propertiesFile = new File(absoluteFilename);
                }
                if ((null != propertiesFile) && propertiesFile.exists()) {
                    properties = PropertiesExtensions.loadProperties(pathAndFilename);
                }
            }
        }
    }

    if (null == properties) {
        propertiesName = new StringBuilder();
        propertiesName.append(clazz.getSimpleName()).append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        final URL url = ClassExtensions.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            properties = PropertiesExtensions.loadProperties(pathAndFilename);
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }
        if ((null != propertiesFile) && propertiesFile.exists()) {
            properties = PropertiesExtensions.loadProperties(pathAndFilename);
        }
    }
    if (properties == null) {
        for (final String string : missedFiles) {
            LOGGER.info(string);
        }
    }

    return properties;
}

From source file:org.smigo.species.vernacular.VernacularHandler.java

public List<Vernacular> getAnyVernaculars(int speciesId, Locale locale) {
    List<Vernacular> ret = vernacularDao.getVernacularBySpecies(speciesId);

    //this is most likely a user species
    if (ret.size() == 1) {
        return ret;
    }//from   w w  w  .  j a  va2s  . c om

    //filter by locale
    Predicate<Vernacular> localeMatcher = vernacular -> vernacular.getLanguage().equals(locale.getLanguage())
            && (vernacular.getCountry().isEmpty() || vernacular.getCountry().equals(locale.getCountry()));
    if (ret.stream().anyMatch(localeMatcher)) {
        return ret.stream().filter(localeMatcher).collect(Collectors.toList());
    }

    //no translation available, return everything
    return ret;
}

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

private String getLocaleKey(final K key, final Locale locale) {
    final StringBuilder sb = new StringBuilder(key.toString());
    if (locale != null && StringUtils.isNotBlank(locale.getLanguage())) {
        sb.append("_");
        sb.append(locale.getLanguage());
        if (StringUtils.isNotBlank(locale.getCountry())) {
            sb.append("_");
            sb.append(locale.getCountry());
        }/*from  w  ww . j a v  a 2 s.  co  m*/
    }
    return sb.toString();
}

From source file:edu.ku.brc.specify.tools.schemalocale.SchemaLocalizerXMLHelper.java

/**
 * @param locale/*ww w .ja  v a 2 s .  c  om*/
 * @return
 */
protected static String makeLocaleKey(final Locale locale) {
    return makeLocaleKey(locale.getLanguage(), locale.getCountry(), locale.getVariant());
}

From source file:com.orange.mmp.i18n.helpers.DefaultInternationalizationManager.java

/**
 * @see com.orange.mmp.i18n.InternationalizationManager#getLocalizationMap(java.lang.String, java.util.Locale)
 *///from  w  w  w.  j  a  v  a 2s  .  co  m
public Map<?, ?> getLocalizationMap(final String messageSource, final Locale locale) {
    final Properties i18nBundle = new Properties();
    final String baseFilePath = i18nFilesPathByMessageSource.get(messageSource);

    //Find name for the i18n file (and verify if file exist)
    File file = null;
    if (locale.getCountry() != null && locale.getCountry().length() > 0) {
        file = new File(baseFilePath + "_" + locale.getLanguage() + "_" + locale.getCountry() + ".properties");
    }
    if (file == null || !file.exists()) {
        file = new File(baseFilePath + "_" + locale.getLanguage() + ".properties");
    }
    if (file == null || !file.exists()) {
        file = new File(baseFilePath + ".properties");
    }

    //Read file (event if file not exist... error is automatically processed via AspectJ and add to log
    final InputStream inStream;
    try {
        inStream = new FileInputStream(file);
        i18nBundle.load(inStream);
    } catch (FileNotFoundException e) {
        //File cannot be opened
        //Add in log via AspectJ
    } catch (IOException e) {
        //Error during read file
        //Add in log via AspectJ
    }

    //Return properties map
    return i18nBundle;
}

From source file:org.shredzone.cilla.plugin.flattr.FlattrLanguage.java

/**
 * Finds a Flattr {@link LanguageId} for the given {@link Locale}. The result is
 * cached. For generic locales (like "es"), an attempt is made to find a matching
 * common Flattr language ("es_ES")./* w ww  .jav a 2  s .c o  m*/
 *
 * @param locale
 *            {@link Locale} to find a {@link LanguageId} for
 * @return {@link LanguageId} or {@code null} if this locale is not supported by
 *         Flattr
 */
public LanguageId findLanguageId(Locale locale) {
    // Is there a locale and language set?
    if (locale == null || locale.getLanguage() == null || locale.getLanguage().isEmpty()) {
        return null;
    }

    // Fetch/update the language set
    updateLanguageSet();

    // Variant is to be ignored completely

    // Check the language/country code (e.g. "en_US") for a perfect match
    if (locale.getCountry() != null && !locale.getCountry().isEmpty()) {
        String result = locale.getLanguage() + '_' + locale.getCountry();
        if (languageSet.contains(result)) {
            return Language.withId(result);
        }
    }

    // Only check the language code (e.g. "fr")
    String language = locale.getLanguage();

    // Check if there is a common transformation available
    String common = transformCommonCodes(language);
    if (languageSet.contains(common)) {
        return Language.withId(common);
    }

    // Find the next language code that starts with the language
    language += '_';

    final String languageF = language;
    return languageSet.stream().filter(it -> it.startsWith(languageF)).map(Language::withId).findFirst()
            .orElse(null); // The language is not supported by Flattr
}

From source file:WelcomeServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    //Get the client's Locale
    Locale locale = request.getLocale();

    ResourceBundle bundle = ResourceBundle.getBundle("i18n.WelcomeBundle", locale);

    String welcome = bundle.getString("Welcome");

    //Display the locale
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html><head><title>" + welcome + "</title></head><body>");

    out.println("<h2>" + welcome + "</h2>");

    out.println("Locale: ");
    out.println(locale.getLanguage() + "_" + locale.getCountry());

    out.println("</body></html>");
    out.close();//w w w.j  av a 2 s  .  c  o  m

}

From source file:org.infoscoop.widgetconf.I18NConverter.java

public I18NConverter(Locale locale, Collection<MessageBundle> resourceBundles) {
    List<MessageBundle> bundles = new ArrayList<MessageBundle>();
    for (MessageBundle bundle : resourceBundles) {
        String lang = bundle.getLocale().getLanguage();
        String country = bundle.getLocale().getCountry();

        if ((lang.equalsIgnoreCase(locale.getLanguage()) || lang.equalsIgnoreCase("ALL"))
                && (country.equalsIgnoreCase(locale.getCountry()) || country.equalsIgnoreCase("ALL")))
            bundles.add(bundle);//from  w w  w . j av  a 2  s  .c  o m
    }

    if (bundles.size() == 0 && resourceBundles.size() > 0)
        bundles.add(resourceBundles.toArray(new MessageBundle[] {})[0]);

    Collections.sort(bundles);

    MessageBundle.Direction direction = null;
    this.replaceMap = new HashMap<String, String>();
    for (MessageBundle bundle : bundles) {
        try {
            replaceMap.putAll(bundle.getMessages());

            direction = bundle.getDirection();
        } catch (Exception ex) {
            logger.error("illegal message bundle", ex);
        }
    }

    this.direction = direction;
    bidiReplaceMap.putAll(getBidiReplaces(direction));
}

From source file:de.alpharogroup.lang.PropertiesUtils.java

/**
 * Load the properties file from the given class object. The filename from the properties file
 * is the same as the simple name from the class object and it looks at the same path as the
 * given class object. If locale is not null than the language will be added to the filename
 * from the properties file./*from  w ww .ja v a 2  s .c  o  m*/
 * 
 * @param clazz
 *            the clazz
 * @param locale
 *            the locale
 * @return the properties
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadPropertiesFromClassObject(final Class<?> clazz, final Locale locale)
        throws IOException {
    if (null == clazz) {
        throw new IllegalArgumentException("Class object must not be null!!!");
    }
    StringBuilder propertiesName = new StringBuilder();
    Properties properties = null;
    String language = null;
    String filename = null;
    String pathAndFilename = null;
    File propertiesFile = null;
    String absoluteFilename = null;
    final String packagePath = PackageUtils.getPackagePathWithSlash(clazz);
    final List<String> missedFiles = new ArrayList<>();
    if (null != locale) {
        propertiesName.append(clazz.getSimpleName());
        language = locale.getLanguage();
        if (null != language && !language.isEmpty()) {
            propertiesName.append("_").append(language);
        }

        final String country = locale.getCountry();
        if (null != country && !country.isEmpty()) {
            propertiesName.append("_").append(country);
        }
        propertiesName.append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        URL url = ClassExtensions.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }

        if (null != propertiesFile && propertiesFile.exists()) {
            properties = PropertiesUtils.loadProperties(pathAndFilename);
        } else {
            propertiesName = new StringBuilder();
            if (null != locale) {
                propertiesName.append(clazz.getSimpleName());
                language = locale.getLanguage();
                if (null != language && !language.isEmpty()) {
                    propertiesName.append("_").append(language);
                }
                propertiesName.append(FileExtension.PROPERTIES.getExtension());
                filename = propertiesName.toString().trim();
                pathAndFilename = packagePath + filename;
                url = ClassExtensions.getResource(clazz, filename);

                if (url != null) {
                    absoluteFilename = url.getFile();
                } else {
                    missedFiles.add("File with filename '" + filename + "' does not exists.");
                }
                if (null != absoluteFilename) {
                    propertiesFile = new File(absoluteFilename);
                }
                if (null != propertiesFile && propertiesFile.exists()) {
                    properties = PropertiesUtils.loadProperties(pathAndFilename);
                }
            }
        }
    }

    if (null == properties) {
        propertiesName = new StringBuilder();
        propertiesName.append(clazz.getSimpleName()).append(FileExtension.PROPERTIES.getExtension());
        filename = propertiesName.toString().trim();
        pathAndFilename = packagePath + filename;
        final URL url = ClassExtensions.getResource(clazz, filename);

        if (url != null) {
            absoluteFilename = url.getFile();
        } else {
            properties = PropertiesUtils.loadProperties(pathAndFilename);
            missedFiles.add("File with filename '" + filename + "' does not exists.");
        }

        if (null != absoluteFilename) {
            propertiesFile = new File(absoluteFilename);
        }
        if (null != propertiesFile && propertiesFile.exists()) {
            properties = PropertiesUtils.loadProperties(pathAndFilename);
        }
    }
    if (properties == null) {
        for (final String string : missedFiles) {
            LOGGER.info(string);
        }
    }

    return properties;
}