Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to know the language by default by country on Locale?

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.

like image 873
ovejaexiste Avatar asked Jul 29 '13 10:07

ovejaexiste


People also ask

How do I get language code from locale?

The getLanguage() method of Locale class in Java is used to get language code for the specified locale. This will either be an empty string or a lowercase ISO 639 code. Parameters: This method does not take any parameters.

Does locale include language?

Locale is a lightweight object that contains only a few important members: A language code. An optional country or region code. An optional variant code.

What is use locale default?

When the Java virtual machine starts running on a computer, it creates an object called the default locale. This object affects the format of strings created from data. Depending on the default locale, a program creates different strings for the same number.

How does JVM determine default locale?

That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.


1 Answers

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);  
like image 192
minus Avatar answered Nov 12 '22 09:11

minus