10

I have these lines of code:

Locale[] cosas = Locale.getAvailableLocales();

for (int i = 0; i < cosas.length; i++) {
    log.info(cosas[i]);
}

I obtain this list:

ms_MY
ar_QA
is_IS
fi_FI
pl
en_MT
it_CH
nl_BE
ar_SA
ar_IQ
es_PR
es_CL
fi
de_AT
da
en_GB
es_PA
sr
ar_YE
mk_MK
mk
en_CA
vi_VN
nl_NL
es_US
zh_CN
es_HN
en_US
fr
th
ar
ar_MA
lv
de
in_ID
hr
en_ZA
ko_KR
ar_TN
in
ja
sr_RS
be_BY
zh_TW
ar_SD
pt
is
ja_JP_JP_#u-ca-japanese
es_BO
ar_DZ
ms
es_AR
ar_AE
fr_CA
sl
es
lt_LT
sr_ME_#Latn
ar_SY
ru_RU
fr_BE
es_ES
bg
iw_IL
sv
en
iw
da_DK
es_CR
zh_HK
zh
ca_ES
th_TH
uk_UA
es_DO
es_VE
pl_PL
ar_LY
ar_JO
it
uk
hu_HU
ga
es_GT
es_PY
bg_BG
hr_HR
sr_BA_#Latn
ro_RO
fr_LU
no
lt
en_SG
es_EC
sr_BA
es_NI
sk
ru
mt
es_SV
nl
hi_IN
et
el_GR
sl_SI
it_IT
ja_JP
de_LU
fr_CH
mt_MT
ar_BH
sq
vi
sr_ME
pt_BR
no_NO
el
de_CH
zh_SG
ar_KW
ar_EG
ga_IE
es_PE
cs_CZ
tr_TR
cs
es_UY
en_IE
en_IN
ar_OM
sr_CS
ca
be
sr__#Latn
ko
sq_AL
pt_PT
lv_LV
sr_RS_#Latn
sk_SK
es_MX
en_AU
no_NO_NY
en_NZ
sv_SE
ro
ar_LB
de_DE
th_TH_TH_#u-nu-thai
tr
es_CO
en_PH
et_EE
el_CY
hu
fr_FR

For example, for Spain as a country, the list contains two locales: es_ES and ca_ES, that is not the same for the Spanish language, of course.

Then, my question is, how can I know which one is the language by default for a country? It is possible to construct the locale just by the language, but I need to pass by parameter for the method only the country and now I have this code to assign a language by default:

if (language.equals("")) {
    switch (country) {
        case "CN":
            language = "zh";
            break;
        case "ES":
            language = "es";
            break;
        case "US":
            language = "en";
            break;
        case "JP":
            language = "ja";
            break;
        default:
            country = "";
            break;
        }
    }

if (language.equals("") && country.equals("")) {
    newLocale = new Locale("es", "ES");
} else {
    newLocale = new Locale(language, country);
}

RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, newLocale);

But I need to extend this switch for a lot of languages more. So, I prefer to have a clear way to assign the language by default for a country.

I found out a solution on this link, but I tried it out and it is not right (for example, for Spain, it returns as default ca-ES, and it is not). Does anybody think it is really possible to obtain the country by code? Any idea? Thank you so much.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ovejaexiste
  • 661
  • 1
  • 6
  • 15
  • 3
    What do you mean by "default language"? If more languages are spoken in one country, there is rarely one language defined as default or more superior. If you mean the most widely used language, there is no data for that in the localization support in Java. – jarnbjo Jul 29 '13 at 12:20
  • Well, this is not by Country, but by **user** look to [`Locale.getDefault()`](http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#getDefault%28%29). – Andrew Thompson Jul 29 '13 at 13:43
  • @jarnbjo JVM lists both `en_US` and `es_US`. Clearly English is the national language of the United States with only about 10% of the population speaking Spanish. – Maarten Jul 11 '19 at 09:52
  • @Maarten As I already wrote: If you mean the most widely used language, there is no data for that in the localization support in Java. I am not sure what you understand with the term 'national language', but English does not have any legal or administrative superiority above Spanish in the USA. USA does not have an offical language on the federal level, but in several states other languages are also used by the administration. In New Mexico, Spanish is used as an administrative/official language, in Hawaii Hawaiian, in Alaska several indigenous languages. – jarnbjo Jul 11 '19 at 10:02
  • @jarnbjo https://en.wikipedia.org/wiki/National_language Most contries have exactly one language which almost all citizens speak, the government communicates in, etc. A default language. Even if it is not specified officially by decree. Saying it "rarely exists" is false. – Maarten Jul 12 '19 at 09:41

3 Answers3

1

You can get the language for a locale like this:

String lang = Locale.getDefault().getISO3Language();

You can also get the country for a locale:

String country = Locale.getDefault().getISO3Country();

Many countries have more than one language. For example, there is a Canadian French and Canadian English locale.

There is not really a 'default' language for any country. The JVM will use the default local of the machine it is on, but the language and country also can be set using the -Duser.country -Duser.languagevariables.

You can also change the default locale programmatically using arbitrary combinations of country and language. For example, this works:

    Locale l = new Locale("Ca", "Cyrl");
    Locale.setDefault(l);  
minus
  • 320
  • 1
  • 5
  • 14
  • "There is not really a 'default' language for any country" Most countries have official national languages or de facto national languages. Compare `en_US` and `es_US`. – Maarten Jul 11 '19 at 09:53
  • `Locale.getDefault()` will return the default of the project, which is not what was asked for. – RiZKiT Jun 26 '20 at 08:52
1

The stance that default languages don't exist is quite false in my opinion -- or a truism that it is not useful to solving the problem at hand. Almost every country has a language that is the dominant or national language, either officially or de facto. Or at least there can be some way of determining precedence for multiple languages.

It is often subjective or domain-specific what the primary language should be and what the weight a particular language should, and it is subject to change. So it makes sense that Java or Unicode does not provide this information. Luckily the number of countries with more than one language is quite small (at least by my JVM locale listing) and all the data we need to determine preference is easily available. So we can manually specify this information.

Below is my (subjective) ranking. Special care must be paid to some edge cases, e.g. Serbian, because Cyrillic is used in official communications but more people can read Latin. So it depends on your use case which one you prefer in some of these cases.

Also take care that the locale repository and language usage might change, so this answer may be outdated.

//    https://en.wikipedia.org/wiki/Languages_of_Ireland
//    English (99%)
//    Irish (36%)
setPreference("IE", "en_IE", 0.99) // English
setPreference("IE", "ga_IE", 0.36) // Irish

// India
//    https://en.wikipedia.org/wiki/Languages_of_India
//    Hindi (57.1%)
//    English (10.6%)
setPreference("IN", "hi_IN", 0.571) // Hindi
setPreference("IN", "en_IN", 0.106) // English

// Bosnia and Herzegovina
//    https://en.wikipedia.org/wiki/Serbian_Cyrillic_alphabet
//    Although the Bosnian language "officially accept[s] both alphabets", the Latin script is almost always used in the Federation of Bosnia and Herzegovina
setPreference("BA", "sr_BA_#Latn", 0.75) // Serbian
setPreference("BA", "sr_BA", 0.25) // Serbian

// Serbia
//    https://en.wikipedia.org/wiki/Serbian_Cyrillic_alphabet
//    Cyrillic is an important symbol of Serbian identity. In Serbia, official documents are printed in Cyrillic only even though, according to a 2014 survey, 47% of the Serbian population write in the Latin alphabet whereas 36% write in Cyrillic.
setPreference("RS", "sr_RS", 0.51) // Serbian
setPreference("RS", "sr_RS_#Latn", 0.5) // Serbian

// Belgium
//    https://en.wikipedia.org/wiki/Languages_of_Belgium
//    Dutch (1st language: ~55%, 2nd language: 16%)
//    French (1st language: ~36%, 2nd language: ~49%)
setPreference("BE", "nl_BE", 0.55) // Dutch
setPreference("BE", "fr_BE", 0.36) // French

// Japan
setPreference("JP", "ja_JP", 0.51) // Japanese
setPreference("JP", "ja_JP_JP_#u-ca-japanese", 0.49) // Japanese

// Singapore
//    https://en.wikipedia.org/wiki/Singapore#Languages
//    Singapore has four official languages: English, Malay, Mandarin Chinese, and Tamil.[333] English is the common language, and is the language of business and government, and the medium of instruction in schools.
setPreference("SG", "en_SG", 0.369) // English
setPreference("SG", "zh_SG", 0.349) // Chinese

// Canada
//    https://en.wikipedia.org/wiki/Languages_of_Canada
setPreference("CA", "fr_CA", 0.5597) // French
setPreference("CA", "en_CA", 0.2061) // English

// Switzerland
//    https://en.wikipedia.org/wiki/Languages_of_Switzerland
//    Year  German  French  Italian Romansh Other
//    2015  63.0    22.7    8.4 0.6 5.3
setPreference("CH", "de_CH", 0.63) // German
setPreference("CH", "fr_CH", 0.227) // French
setPreference("CH", "it_CH", 0.084) // Italian

// Thailand
//    https://docs.oracle.com/javase/tutorial/i18n/locale/extensions.html
//    a Unicode locale extension is specified by the 'u' key code or the UNICODE_LOCALE_EXTENSION constant. The value itself is also specified by a key/type pair. Legal values are defined in the Key/Type Definitions table on the Unicode website. A key code is specified by two alphabetic characters.
//    nu    number type
setPreference("TH", "th_TH", 0.51) // Thai
setPreference("TH", "th_TH_TH_#u-nu-thai", 0.49) // Thai

// Luxembourg
//    https://en.wikipedia.org/wiki/Languages_of_Luxembourg
//    2012  Luxembourgish   French  German  English other
//    Native language   52% 16% 2%  N/A 30%
setPreference("LU", "fr_LU", 0.16) // French
setPreference("LU", "de_LU", 0.02) // German

// Montenegro
//    https://en.wikipedia.org/wiki/Montenegrin_alphabet
//    Although the Latin and Cyrillic alphabets enjoy equal status under the Constitution of Montenegro, the government and proponents of the Montenegrin language prefer to use the Latin script.
setPreference("ME", "sr_ME_#Latn", 0.8) // Serbian
setPreference("ME", "sr_ME", 0.2) // Serbian

// United States
setPreference("US", "en_US", 0.98) // English
setPreference("US", "es_US", 0.88) // Spanish

// Malta
//    https://en.wikipedia.org/wiki/Languages_of_Malta
//    98% of Maltese people can speak Maltese, 88% can speak English
setPreference("MT", "mt_MT", 0.98) // Maltese
setPreference("MT", "en_MT", 0.88) // English

// Spain
//    https://en.wikipedia.org/wiki/Languages_of_Spain
//    the most prominent of the languages of Spain is Spanish (Castilian), spoken by about 99% of Spaniards as a first or second language.[5] Catalan (or Valencian) is spoken by 19%
setPreference("ES", "es_ES", 0.99) // Spanish
setPreference("ES", "ca_ES", 0.19) // Catalan

// Norway
//    https://en.wikipedia.org/wiki/Languages_of_Switzerland
//    Bokmål is the preferred written standard of Norwegian for 85% to 90%
//    Nynorsk is reportedly used as main form of Norwegian by around 7.4% of the total population
setPreference("NO", "no_NO", 0.85) // Norwegian
setPreference("NO", "no_NO_NY", 0.074) // Norwegian

// Greece
//    The official language of Greece is Greek, spoken by 99% of the population
//    English (51%)
//    German (9%)
//    French (8.5%)
//    Italian (8%)
setPreference("GR", "el_GR", 0.99) // Greek
setPreference("GR", "de_GR", 0.09) // German

More information:

Maarten
  • 6,894
  • 7
  • 55
  • 90
  • I mean, it's a great idea... just needs to be added into a library now for convenience :p – ArcX Jul 28 '21 at 11:19
0

Does it have to be java? I made a small exe in c++ that you might find useful: KyaaLocale, you can customize it as you want, adding more if/then/elses or whatever you need.

Arco
  • 103
  • 1
  • 1
  • 6
  • Thank you, @Kyousuke Kyaa. Yes, I need it to be Java code. I took a look on yours, and I think it is not possible to transform it to Java code, so I need another solution. – ovejaexiste Jul 29 '13 at 11:52