How to get all Java supported Locales

南笙酒味 提交于 2020-07-07 09:57:45

问题


When Google you will find lot of materials to find all the supported Locales by Java. But its all confusing.

For example [http://sanjaal.com/java/tag/java-locale-tutorial/] show an output of 210 locales. But when I run the same program I get only 155. I don;t get for example ta_IN. si_LK is not output by any program.

Can someone please clear the air?

I use JDK/JRE 1.7

http://www.oracle.com/technetwork/java/javase/javase7locales-334809.html gives 111 entries.

I have a Spring Application which supports i18n and our customers can put their own localisations. What I am trying to do is to provide a list of all locales for them to select their one from.

Oh! this is confusing. Local.getISOCountries() provide LK as a country and Locale.getISOLanguages(); provide si as a language .... but si_LK which is a valid locale is not given in Locale.getAvailableLocales();


回答1:


    Locale loc = new Locale("ta", "IN"); // ta_IN, si_LK
    System.out.printf("Name: %s%n"
            + "Country: %s; %s - %s%n"
            + "Language: %s; %s - %s%n"
            + "Script: %s - %s%n",
            loc.getDisplayName(Locale.ENGLISH),
            loc.getCountry(), loc.getISO3Country(), loc.getDisplayCountry(Locale.ENGLISH),
            loc.getLanguage(), loc.getISO3Language(), loc.getDisplayLanguage(Locale.ENGLISH),
            loc.getScript(), loc.getDisplayScript(Locale.ENGLISH));

Name: Tamil (India)
Country: IN; IND - India
Language: ta; tam - Tamil
Script:  - 

Name: Sinhalese (Sri Lanka)
Country: LK; LKA - Sri Lanka
Language: si; sin - Sinhalese
Script:  - 

Also it is possible to provide support for ones own locale (since Java 7 I believe). I made it for Esperanto, and it is doable (LocaleProvider). But in your case all might be there.


    SimpleDateFormat f = new SimpleDateFormat("EEEE", loc);
    System.out.println("Weekday: " + f.format(new Date()));

Unfortunately shows "Tuesday," so one needs to implement the language formats and such. I found a project for serbo-croatian/bosnian.




回答2:


The code you found on internet use the next function to extract all the locales:

Locale list[] = DateFormat.getAvailableLocales();

If we read the documentation for that function:

Returns an array of all locales for which the get*Instance methods of this class can return localized instances. The returned array represents the union of locales supported by the Java runtime and by installed DateFormatProvider implementations. It must contain at least a Locale instance equal to Locale.US

So we can understand that the number of locales returned depends on your runtime version, and your DateFormatProvider.

For example in my machine with JDK 1.6.0_27 I get 152 results.




回答3:


To provide a list of all ISO countries in your JAVA Version try the following:

public static void main(String[] args) {

    String[] locales = Locale.getISOCountries();
    for (String countryCode : locales) {
        Locale obj = new Locale("", countryCode);
        System.out.println("Country Code = " + obj.getCountry() 
            + ", Country Name = " + obj.getDisplayCountry());
    }
}

This way you can provide a complete list of all countries but it is still dependend on the current ISO country list of your JDK.

Another way would be to use a REST-API from a provider like: https://restcountries.eu/rest/v2/all from https://restcountries.eu/

This way your program is independent of the JDK you use and is always up2date (if your chosen provider is reliable).



来源:https://stackoverflow.com/questions/17292575/how-to-get-all-java-supported-locales

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!