Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing Locales in Java? [duplicate]

Tags:

java

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

like image 937
Steve B. Avatar asked Jan 21 '10 17:01

Steve B.


People also ask

How do I get the locale in Java?

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.

What is locales in Java?

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.

What is locale root in Java?

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.


4 Answers

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.

like image 149
McDowell Avatar answered Oct 02 '22 08:10

McDowell


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

like image 40
Aadith Ramia Avatar answered Sep 30 '22 08:09

Aadith Ramia


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.

like image 41
Tendayi Mawushe Avatar answered Oct 04 '22 08:10

Tendayi Mawushe


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.

like image 45
mindas Avatar answered Oct 01 '22 08:10

mindas