下面列出了java.util.Locale#getISOCountries ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* @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!");
}
/**
* @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!");
}
/**
* @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!");
}
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;
}
}
/**
* 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);
}
/**
* @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!");
}
/**
* @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!");
}
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;
}
/**
* 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;
}
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 "";
}
/**
* 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;
}
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;
}
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);
}
}
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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);
}
}