当前位置:   article > 正文

java display.getdefault(),为什么Locale.getDefault()。使用getLanguage()Android中返回的语言code中的显示名称呢?...

locale.getdefault().getlanguage()

According to the Java reference, Locale.getLanguage() is supposed to return the 2-letters lowercase ISO code of the language (e.g. "en"), while getDisplayLanguage() is the method for obtaining the readable name (e.g. "English").

So how comes that the following code in Android:

Locale.getDefault().getLanguage()

returns "English" or "Español" instead of "en" and "es"????

I'm completely puzzled...

解决方案

I've figured it out. This happened because I had previously called Locale.setDefault() and passed it a Locale which in turn I had created by erroneously passing it the whole language name (I took the language from a preference setting and I mistakenly picked the label of the entry instead of the value).

That is, I did:

String lang= //... here I assigned "English" while I thought

// I was assigning it "en"

Locale locale=new Locale(lang);

Locale.setDefault(locale); // (*)

// and later

Locale.getLocale().getLanguage(); //returns "english"

So when I queried for the default locale, it actually was the locale I had created whose language code I had erroneously set to "english".

There are a couple of funny things, though:

The line (*) actually works and actually does change the locale to English (or to Spanish when I used "Spanish"), that is, setDefault() seems to accept a "malformed" locale and even understands it. But it doesn't fix it.

Note I used uppercase English when wrongly setting the locale, but at the end it returns "english" all lowercase.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/255550
推荐阅读
相关标签
  

闽ICP备14008679号