我有这些代码行:
Locale[] cosas = Locale.getAvailableLocales();
for (int i = 0; i < cosas.length; i++) {
log.info(cosas[i]);
}
我获得此列表:
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
例如,对于西班牙国家/地区,该列表包含两个区域设置:es_ES和ca_ES,当然,这与西班牙语不同。
然后,我的问题是,我怎么知道一个国家默认哪种语言?可以仅通过语言构造语言环境,但我需要通过该方法的参数传递国家,现在我有这个代码默认分配语言:
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);
但我需要为更多语言扩展此开关。因此,我更倾向于为一个国家/地区默认分配语言。
我在这个link上找到了一个解决方案,但是我尝试了它并且它不对(例如,对于西班牙,它返回默认的ca-ES,而不是)。有人认为通过代码获取国家真的有可能吗?任何的想法?非常感谢你。
答案 0 :(得分:0)
它必须是java吗?我在c ++中创建了一个你可能觉得有用的小exe:KyaaLocale,你可以根据需要自定义它,添加更多if/then/else
或你需要的任何东西。
答案 1 :(得分:0)
您可以获取以下语言环境的语言:
String lang = Locale.getDefault().getISO3Language();
您还可以获取该地区的国家/地区:
String country = Locale.getDefault().getISO3Country();
许多国家/地区都使用多种语言。例如,有加拿大法语和加拿大英语语言环境。
任何国家/地区都没有真正的“默认”语言。 JVM将使用其所在机器的默认本地,但也可以使用-Duser.country -Duser.language
变量设置语言和国家/地区。
您还可以使用国家/地区和语言的任意组合以编程方式更改默认语言环境。例如,这有效:
Locale l = new Locale("Ca", "Cyrl");
Locale.setDefault(l);
答案 2 :(得分:0)
在我看来,默认语言不存在的立场是很愚蠢的。几乎每个国家都使用官方或事实上的主导语言或本国语言。
幸运的是,使用一种以上语言的国家/地区的数量非常少(至少从我的JVM语言环境列表中可以看出),并且我们可以轻松确定所需的所有数据。因此,我们可以手动指定此信息。以下是我的(主观)排名。在某些情况下必须格外小心,例如塞尔维亚语,因为西里尔文用于官方通讯,但更多的人可以阅读拉丁文。因此,在某些情况下,这取决于您的用例。
还请注意,语言环境存储库和语言使用可能会发生变化,因此此答案可能已过时。
// 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
更多信息: