I've found a number of missing countries in java locales- If I print out a list of available locales,
TreeSet< String > m = new TreeSet< String >();
for ( Locale l : Locale.getAvailableLocales() ) {
m.add( l.getDisplayCountry() );
}
for ( String s : m ) {
System.out.println( s );
}
I'm missing a number of countries, including Nigeria, Iran, Kyrgyzstan, and Pakistan. Can anyone shed any light on this, or is there a better(more comprehensive) way of getting a country list in java?
1.6.0_16-b01
To get equivalent information in Java, use Locale. getDefault() to get the Locale that Java is using, and use methods on the Locale object such as getCountry() , getLanguage() to get details. The information is available using ISO codes and as human readable/displayable names.
A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.
The root locale is the locale whose language, country, and variant are empty ("") strings. This is regarded as the base locale of all locales, and is used as the language/country neutral locale for the locale sensitive operations.
Sun Java 6 only provides support for a limited subset of locales. The vector of support for formatting classes/writing systems/etc. is listed in the JDK documentation.
Now, I haven't done this, but...
You can plug in support for additional locales via the SPIs (described here). For example, to provide a date formatter for a new locale, you would do it by implementing a DateFormatProvider
service. You might be able to do this by decorating an existing implementation - I'd have a look at the ICU4J library to see if it provides the support you want.
there is no one-to-one mapping between countries in the world and the locales. Locales specify regions. They are meant to mark things like language diversity. Just a clarifying example : India, Sri Lanka, Pakistan , Bangladesh are all different countries..but the way they use the english language is not different in any significant way..its a demographic thing..so all these countries could depends on the indian locale
The static method Locale.getISOCountries()
returns all the two letter codes for countries in the ISO 3166 Countries list. If you run the code below:
String[] codes = Locale.getISOCountries();
for (String code : codes) {
System.out.println(code);
}
You will notice that PK (PAKISTAN), NG (NIGERIA) and KG (KYRGYZSTAN) are all in the list. Note this does not mean the locale associated with the country is installed. If you want to implement any locale specific behaviour the only locales supported are those returned by Locale.getAvailableLocales
.
If you want to support more locales/countries than are currently available in Sun JDK, consider adding ICU4J jars to your java.ext.dirs
- you will not only get far more complete list of countries and locales but also decent support for Collator
, DateFormat
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With