I am trying to get all the locales from the JVM to populate a Country drop down. The first item is an empty not null object. It isn't null because I am using a TreeMap Collection to add the country Abbreviations and (Displayable) Names. Look below to see what the collection is.
{=, AE=United Arab Emirates, AL=Albania, AR=Argentina, AT=Austria, AU=Australia, BA=Bosnia and Herzegovina, BE=Belgium, BG=Bulgaria, BH=Bahrain, BO=Bolivia, BR=Brazil,....
Here's the code. I remove that empty object to make sure the first value is not empty.
public Map<String, String> countries(Locale currentLocale) {
Map<String, String> countries = new TreeMap<String, String>();
for (Locale locale : Locale.getAvailableLocales()) {
countries.put(locale.getCountry(),
locale.getDisplayCountry(currentLocale));
}
countries.remove("");
return countries;
}
JVM version - (build 1.7.0_09-b05)
The javadocs basically cover this.
For
locale.getCountry()
you find:And for
locale.getDisplayCountry()
you find:The one you can't "see" doesn't have an ISO code and is falling back to that because it can't be localized to your locale and doesn't have an english name either (or, doesn't have a country specified). It is perfectly valid for you to wind up with empty strings for both (though, fairly useless).
try
you will see that some Locales have only language and no country
The locale in question is none other than Locale.ROOT.
locale == Locale.ROOT
on my end, and I assume that the same will be true for you.Why does
Locale.ROOT
exist? According to http://www.oracle.com/technetwork/java/javase/java8locales-2095355.html: