Java Code Examples for java.util.Locale#getISOCountries()

The following examples show how to use java.util.Locale#getISOCountries() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: OpenEstate-IO   File: LocaleUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Translate a country name into another language.
 *
 * @param country  country name
 * @param language language to translate
 * @return translated country name or null, if no translation was found
 */
public static String translateCountryName(String country, Locale language) {
    country = StringUtils.trimToNull(country);
    if (country == null) return null;
    for (String iso2Code : Locale.getISOCountries()) {
        Locale countryLocale = new Locale(iso2Code, iso2Code);
        for (Locale translationLocale : LocaleUtils.availableLocaleList()) {
            String name = StringUtils.trimToNull(countryLocale.getDisplayCountry(translationLocale));
            if (name != null && name.equalsIgnoreCase(country)) {
                name = StringUtils.trimToNull(countryLocale.getDisplayCountry(language));
                if (name != null) return name;
            }
        }
    }
    return null;
}
 
Example 2
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 3
Source Project: jdk8u60   File: LocaleTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 4
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 5
Source Project: CountryCurrencyPicker   File: Country.java    License: Apache License 2.0 6 votes vote down vote up
public static ArrayList<Country> listAll(Context context, final String filter) {
    ArrayList<Country> list = new ArrayList<>();

    for (String countryCode : Locale.getISOCountries()) {
        Country country = getCountry(countryCode, context);

        list.add(country);
    }

    sortList(list);

    if (filter != null && filter.length() > 0) {
        return new ArrayList<>(Collections2.filter(list, new Predicate<Country>() {
            @Override
            public boolean apply(Country input) {
                return input.getName().toLowerCase().contains(filter.toLowerCase());
            }
        }));
    } else {
        return list;
    }
}
 
Example 6
Source Project: j2objc   File: LocaleTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * java.util.Locale#getISOCountries()
 */
public void test_getISOCountries() {
    // Test for method java.lang.String []
    // java.util.Locale.getISOCountries()
    // Assumes all countries are 2 digits, and that there will always be
    // 230 countries on the list...
    String[] isoCountries = Locale.getISOCountries();
    int length = isoCountries.length;
    int familiarCount = 0;
    for (int i = 0; i < length; i++) {
        if (isoCountries[i].length() != 2) {
            fail("Wrong format for ISOCountries.");
        }
        if (isoCountries[i].equals("CA") || isoCountries[i].equals("BB")
                || isoCountries[i].equals("US")
                || isoCountries[i].equals("KR"))
            familiarCount++;
    }
    assertTrue("ISOCountries missing.", familiarCount == 4 && length > 230);
}
 
Example 7
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 8
/**
 * @bug 4126880
 */
void Test4126880() {
    String[] test;

    test = Locale.getISOCountries();
    test[0] = "SUCKER!!!";
    test = Locale.getISOCountries();
    if (test[0].equals("SUCKER!!!"))
        errln("Changed internal country code list!");

    test = Locale.getISOLanguages();
    test[0] = "HAHAHAHA!!!";
    test = Locale.getISOLanguages();
    if (test[0].equals("HAHAHAHA!!!")) // Fixed typo
        errln("Changes internal language code list!");
}
 
Example 9
public static Map<String, String> getCountries() {
    Map<String, String> countries = new HashMap<String, String>();

    String[] isoCountries = Locale.getISOCountries();
    for (String country : isoCountries) {
        Locale locale = new Locale("", country);
        String iso = locale.getISO3Country();
        String code = locale.getCountry();
        String name = locale.getDisplayCountry();

        if (!"".equals(iso) && !"".equals(code)
                && !"".equals(name)) {
            countries.put(code, name);
        }
    }

    return countries;
}
 
Example 10
Source Project: OpenEstate-IO   File: LocaleUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return an ISO-3 country code from a country name.
 *
 * @param country country name
 * @return ISO-3 country code or null, if no code was found
 */
public static String getCountryISO3(String country) {
    country = StringUtils.trimToNull(country);
    if (country == null)
        return null;
    if (country.length() == 3)
        return country;

    String[] iso2Codes = Locale.getISOCountries();
    if (country.length() == 2) {
        String iso3code = LocaleUtils.getCountryISO3FromISO2(country);
        if (iso3code != null) return iso3code;
    }

    for (String iso2Code : iso2Codes) {
        Locale countryLocale = new Locale(iso2Code, iso2Code);
        String iso3Code = StringUtils.trimToNull(countryLocale.getISO3Country());
        if (iso3Code == null) continue;
        for (Locale translationLocale : LocaleUtils.availableLocaleList()) {
            String name = StringUtils.trimToNull(countryLocale.getDisplayCountry(translationLocale));
            if (name != null && name.equalsIgnoreCase(country)) return iso3Code;
        }
    }
    return null;
}
 
Example 11
Source Project: smartcoins-wallet   File: Helper.java    License: MIT License 6 votes vote down vote up
public static String getCountryCode(String spinnerText) {
    String[] locales = Locale.getISOCountries();
    ArrayList<String> countries = new ArrayList<>();
    for (String countryCode : locales) {

        Locale locale = new Locale("", countryCode);
        try {
            Currency currency = Currency.getInstance(locale);
            String proposedSpinnerText = locale.getDisplayCountry() + " (" + currency.getCurrencyCode() + ")";

            if (proposedSpinnerText.equals(spinnerText)) {
                return countryCode;
            }
        } catch (Exception e) {

        }
    }
    return "";
}
 
Example 12
Source Project: development   File: CountryBean.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns a mapping from country codes in ISO 3166 to localized country
 * names.
 */
public Map<String, String> getDisplayCountries() {
    if (hasLocaleChanged()) {
        reset();
    }
    if (displayCountries.isEmpty()) {
        Locale userLocale = getCurrentUserLocale();
        for (String code : Locale.getISOCountries()) {
            String country = getDisplayCountry(code, userLocale);
            displayCountries.put(code, country);
        }
    }
    return displayCountries;
}
 
Example 13
Source Project: CountryPicker   File: CountryPicker.java    License: Apache License 2.0 5 votes vote down vote up
private static List<Country> getAllCountries(List<String> userCountryCodes) {
  List<Country> countries = new ArrayList<Country>();

  for (String countryCode : Locale.getISOCountries()) {
    if (userCountryCodes.contains(countryCode)) {
      Country country = new Country();
      country.code = countryCode;
      country.name = new Locale("", countryCode).getDisplayCountry();
      countries.add(country);
    }
  }

  return countries;
}
 
Example 14
Source Project: development   File: Organizations.java    License: Apache License 2.0 5 votes vote down vote up
public static void supportAllCountries(DataService mgr, Organization org) {
    for (String countryCode : Locale.getISOCountries()) {
        SupportedCountry country = SupportedCountries
                .find(mgr, countryCode);
        if (country != null) {
            org.setSupportedCountry(country);
        }
    }
}
 
Example 15
/**
 * This method checks that ISO3166-3 country codes which are PART3 of
 * IsoCountryCode enum, are retrieved correctly.
 */
private static void checkISO3166_3Codes() {
    Set<String> iso3166_3Codes = Locale.getISOCountries(IsoCountryCode.PART3);
    if (!iso3166_3Codes.equals(ISO3166_3EXPECTED)) {
        reportDifference(iso3166_3Codes, ISO3166_3EXPECTED);
    }
}
 
Example 16
/**
 * This method checks that ISO3166-1 alpha-3 country codes which are
 * PART1_ALPHA3 of IsoCountryCode enum, are retrieved correctly.
 */
private static void checkISO3166_1_Alpha3Codes() {
    Set<String> iso3166_1_Alpha3Codes = Locale.getISOCountries(IsoCountryCode.PART1_ALPHA3);
    if (!iso3166_1_Alpha3Codes.equals(ISO3166_1_ALPHA3_EXPECTED)) {
        reportDifference(iso3166_1_Alpha3Codes, ISO3166_1_ALPHA3_EXPECTED);
    }
}
 
Example 17
/**
 * This method checks that ISO3166-1 alpha-2 country codes, which are
 * PART1_ALPHA2 of IsoCountryCode enum, are retrieved correctly.
 */
private static void checkISO3166_1_Alpha2Codes() {
    Set<String> iso3166_1_Alpha2Codes = Locale.getISOCountries(IsoCountryCode.PART1_ALPHA2);
    Set<String> ISO3166_1_ALPHA2_EXPECTED = Set.of(Locale.getISOCountries());
    if (!iso3166_1_Alpha2Codes.equals(ISO3166_1_ALPHA2_EXPECTED)) {
        reportDifference(iso3166_1_Alpha2Codes, ISO3166_1_ALPHA2_EXPECTED);
    }
}
 
Example 18
Source Project: OpenEstate-IO   File: LocaleUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create an ISO-2 country code from an ISO-3 country code.
 *
 * @param iso3Code ISO-3 country code
 * @return ISO-2 country code or null, if no code was found
 */
public static String getCountryISO2FromISO3(String iso3Code) {
    iso3Code = StringUtils.trimToNull(iso3Code);
    if (iso3Code == null) return null;
    if (iso3Code.length() == 3) {
        for (String iso2Code : Locale.getISOCountries()) {
            Locale countryLocale = new Locale(iso2Code, iso2Code);
            String countryISO3 = StringUtils.trimToNull(countryLocale.getISO3Country());
            if (countryISO3 != null && countryISO3.equalsIgnoreCase(iso3Code)) {
                return iso2Code;
            }
        }
    }
    return null;
}
 
Example 19
Source Project: edx-app-android   File: LocaleUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Provides the list of countries fetched from the system.
 *
 * @return A list of {@link FormOption} containing country names and their codes.
 */
public static List<FormOption> getCountries() {
    final List<FormOption> countries = new ArrayList<>();
    for (String countryCode : Locale.getISOCountries()) {
        try {
            countries.add(new FormOption(getCountryNameFromCode(countryCode), countryCode));
        } catch (InvalidLocaleException e) {
            e.printStackTrace();
        }
    }
    sortBasedOnLocale(countries);
    return countries;
}
 
Example 20
Source Project: development   File: SupportedCountries.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates all SupportedCountry objects. Normally all SupportedCountry
 * objects are created during DB setup. Creating all countries is slow and
 * should be done only if needed.
 */
public static void createAllSupportedCountries(DataService mgr)
        throws NonUniqueBusinessKeyException {
    for (String countryCode : Locale.getISOCountries()) {
        findOrCreate(mgr, countryCode);
    }
}