Is there a way to know the default language by country in Locale?

I have these lines of code:

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

I get 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 consists of two locales: es_ES and ca_ES, this is not the same for the Spanish language, of course.

Then, my question is: how can I find out what is the default language for the country? You can build a locale only by language, but I need to pass only the country according to the parameter for the method, and now I have this code to assign the default language:

 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 expand this switch for more languages. Therefore, I prefer to have a clear way to set the default language for the country.

I found a solution on this link , but I tried it, and it is wrong (for example, for Spain it returns as default ca-ES, but it is not). Does anyone think that it is really possible to get a country by code? Any ideas? Thank you very much.

+7
source share
3 answers

You can get the language for the locale, for example:

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

You can also get the country for the locale:

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

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

In fact, the default language is not used for any country. The JVM will use the local default for the machine on which it is enabled, but the language and country can also be set using the variables -Duser.country -Duser.language .

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

  Locale l = new Locale("Ca", "Cyrl"); Locale.setDefault(l); 
+1
source

Do I need to be java? I made a small C ++ exe that may come in handy: KyaaLocale , you can configure it the way you want by adding more if/then/else or whatever you need.

0
source

The position that languages ​​do not exist by default is, in my opinion, rather idiotic. Almost every country has a language that is the dominant or national language, officially or de facto.

Fortunately, the number of countries with more than one language is quite small (at least because of my list of JVM locales), and all the data we need to determine preferences is easily accessible. Thus, we can manually specify this information. Below is my (subjective) rating. Particular attention should be paid to some extreme cases, for example, Serbian, because Cyrillic is used in official communications, but more people can read in Latin. So it depends on your use case, which you prefer in some of these cases.

Also make sure that the locale repository and language usage can 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 

Additional Information:

0
source

Source: https://habr.com/ru/post/950514/


All Articles