开发者

Missing Locales in Java? [duplicate]

开发者 https://www.devze.com 2022-12-17 21:59 出处:网络
This question already has answers here: 开发者_运维知识库 How to use unsupported Locale in Java
This question already has answers here: 开发者_运维知识库 How to use unsupported Locale in Java (2 answers) Closed 2 years ago.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号