3

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();

Ish
  • 171
  • 1
  • 2
  • 10
  • 4
    "Can someone please clear the air?" Well, open the airlock and have a look at the constants declared in `java.util.Locale` – Marco Forberg Jun 25 '13 at 08:30
  • 2
    thanks @MarcoForberg. But he opened airlock brought in smelly air. It has about 15 or so locals. Do you think thats all Java is supporting? – Ish Jun 25 '13 at 08:44
  • No. That are the ones "naturely" supported by the Locale class. And i don't think your question can be answered to a full extend since it is somewhat up to the different classes what Locales they support – Marco Forberg Jun 25 '13 at 08:48
  • As I updated in the question documentation provides 111 locales as supported but if you use Locale localeLK = new Locale("si", "LK"); Locale localeTM = new Locale("ta", "IN"); and use it in simpledateformat Java happily accept them .......... – Ish Jun 25 '13 at 08:51
  • sure. Wouldn't make much sense if SDF refused any Locale, would it? – Marco Forberg Jun 25 '13 at 08:52
  • @MarcoForberg I can;t see a use of java.util.spi.LocaleServiceProvider's isSupportedLocale() method in that case. Unfortunately my dumb mind cannot figure out how that method is working. – Ish Jun 25 '13 at 09:01
  • for the more recent JDK (11): https://www.oracle.com/java/technologies/javase/jdk11-suported-locales.html – Joshua Goldberg Jun 29 '20 at 18:52

3 Answers3

3
    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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • thanks mate.. you are 100% correct! but not to the problem I have. As you have shown ta_IN and si_LK are working in Java. But when I use SimpleDateFormat.getAvailableLocales(), they are not shown. Why? and is there a way to get all possible locals which we can use in Java. – Ish Jun 25 '13 at 09:11
  • **It seems there is no date format** for Sinhalese, maybe do an internet search for any that did implement it. I extended my answer with an example. The JRE jars might help too. – Joop Eggen Jun 25 '13 at 09:34
  • Is the Esperanto Locale provider that you created open sourced somewhere? I'm in need of a provider for Esperanto. – johnlcox Dec 09 '15 at 14:55
  • @johnlcox there obvious is a problem with Esperanto: no country or currency, in general ISO date yyyy-mm-dd. Hence I did not think it worth as mini-project. Also I have seen a couple of E-o locales. Let me look for a while. – Joop Eggen Dec 09 '15 at 17:16
  • @johnlcox I did not find my sources, made a new jre/lib/ext LocaleServiceProvider jar, but the new java 8 DateTimeFormatter did not work. So more is needed. Sorry. – Joop Eggen Dec 09 '15 at 21:57
1

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.

maqjav
  • 2,310
  • 3
  • 23
  • 35
  • thanks @maqjav As I updated in the question J7SE supports 111 locales, so what you are saying is apart from those 111 there will be more coming from my 'DateFormatProvider' which is Oracle's java.text.SimpleDateFormat. Why it has more than J7SE proivdes? – Ish Jun 25 '13 at 08:42
  • @Ish honestly I don't really know, maybe they have removed locales that aren't that common or maybe even locales that are disappearing with the time? – maqjav Jun 25 '13 at 08:46
0

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).

the hand of NOD
  • 1,639
  • 1
  • 18
  • 30