Are Java Locale IDs different for each country?

1.5k Views Asked by At

By looking at Spanish-related locale IDs at Oracle

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

Spanish Argentina   es_AR
Spanish Bolivia es_BO
Spanish Chile   es_CL
Spanish Colombia    es_CO
Spanish Costa Rica  es_CR
Spanish Dominican Republic  es_DO
Spanish Ecuador es_EC
Spanish El Salvador es_SV
Spanish Guatemala   es_GT
Spanish Honduras    es_HN
Spanish Mexico  es_MX
Spanish Nicaragua   es_NI
Spanish Panama  es_PA
Spanish Paraguay    es_PY
Spanish Peru    es_PE
Spanish Puerto Rico es_PR
Spanish Spain   es_ES
Spanish United States   es_US
Spanish Uruguay es_UY
Spanish Venezuela   es_VE

I feel Java Locale IDs are different for each country. Put it another way, two countries do not share the same Locale ID. I understand that a country may have two or more locales.

Is my understanding right? I am unable to find clear description to support my understanding.

Thanks!

1

There are 1 best solutions below

4
On BEST ANSWER

A Locale may include a country code, but they aren't intended to identify countries.

A Locale is intended to represent a combination of rules for displaying translations/formatting/etc.

Locales are identified with a combination of language, country, and variant.

So:

  • en would be English
  • en_US would be the variant of English spoken in America
  • en_US_WIN might represent American English for a version of the software running on Windows (e.g. if you had text that needed to be different for Windows users).

The language and country parts are based on ISO standards. But Locale doesn't really restrict what you do with the variant part.

You should not think of locales as having a one-to-one mapping with countries (or with languages).

e.g.

  • fr_FR = Variety of French spoken in France.
  • fr_CH = Variety of French spoken in Switzerland.
  • de_CH = Variety of German spoken in Switzerland.

Many systems that use Locales will follow a practice of falling back to more general Locales as needed. e.g. ResourceBundle which can be used to look up appropriate text translations based on a message key. If you told it to find a message in Locale en_US_WIN and it didn't find it, it would then try with Locale en_US and then en and then fall back on the 'root' Locale. This means that if American and British English translations used the same text for something, then their translation would only need to be recorded once. It also means that if the code doesn't know about a language it can fall back to a default language (stored in files for the root locale).

If you're interested in getting a list of countries or languages, there are static methods on Locale to help with this.

Locale.getISOCountries();  // Returns list of ISO country codes
Locale.getISOLanguages();  // Returns list of ISO language codes

There are also methods for getting human readable names of languages:

Locale.ENGLISH.getDisplayLanguage(Locale.GERMAN); //Englisch
Locale.GERMAN.getDisplayLanguage(Locale.ENGLISH); //German
Locale.ENGLISH.getDisplayLanguage(Locale.English); //English
Locale.GERMAN.getDisplayLanguage(Locale.GERMAN); //Deutsch

And human readable names of countries:

Locale.US.getDisplayCountry(Locale.ENGLISH); //United States
Locale.GERMANY.getDisplayCountry(Locale.ENGLISH); //Germany

Locale.US.getDisplayCountry(Locale.GERMAN); //Vereinigte Staaten von Amerika
Locale.GERMANY.getDisplayCountry(Locale.GERMAN); //Deutschland