11

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

Steve B.
  • 55,454
  • 12
  • 93
  • 132
  • I suspect that State Department export restrictions have at least something to do with that list... – kdgregory Jan 21 '10 at 17:20
  • 1
    But regardless, I question whether the locale table is a reasonable way to build a list of countries. It depends on your usage, of course, but I think that most applications would be better off with a smaller list, while anything that has to deal with historical data needs a much larger list. – kdgregory Jan 21 '10 at 17:23
  • Makes me curious if there's a way to somehow add your own Locale to the list... – Stroboskop Jan 21 '10 at 17:31
  • Note, the JDK supports more locales than the JRE. – Thorbjørn Ravn Andersen Jan 21 '10 at 19:55
  • @kdgregory - Only indirectly. The real reason will be that Sun / Oracle did not see sufficient (paying) customer demand to justify the effort (== cost) of implementing and maintaining the Locales. – Stephen C Oct 10 '20 at 06:32

4 Answers4

6

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.

Tomasz Szymulewski
  • 2,003
  • 23
  • 23
McDowell
  • 107,573
  • 31
  • 204
  • 267
4

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

Aadith Ramia
  • 10,005
  • 19
  • 67
  • 86
  • Yes, I know there's not really a conceptual 1-1 mapping between locales and countries; Nevertheless, I was hoping (assuming?) that I was getting a complete list (didn't look closely enough, I guess). The ISO list that the javadoc references also misses Pakistan (http://www.loc.gov/standards/iso639-2/php/English_list.php), but then again it's a list of Languages, not Countries. – Steve B. Jan 21 '10 at 17:25
2

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.

Tendayi Mawushe
  • 25,562
  • 6
  • 51
  • 57
  • @Tendayi Mawushe - the country codes might be present, but that doesn't mean that date/number formatters (for example) will automatically provide the required functionality. – McDowell Jan 21 '10 at 18:08
  • 1
    Yes, PK is there, but as far as I can tell you can't do anything but get the 2-letter ISO code. You can't, for example, get a country name keyed from the 2-letter code. – Steve B. Jan 21 '10 at 19:20
  • I am aware that this does not mean the Locale for the country in question is installed. However the question seems to be trying to just be getting a list of countries rather doing any locale specific behaviour. I will make that clear in the answer. – Tendayi Mawushe Jan 22 '10 at 10:12
1

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.

mindas
  • 26,463
  • 15
  • 97
  • 154