为什么Locale.getDefault()。使用getLanguage()Android中返回的语言code中的显示名称呢? [英] Why does Locale.getDefault().getLanguage() in Android return the display name instead of the language code?
问题描述
根据Java参考,Locale.getLanguage()应该返回2-字母小写的ISO $ C $语言的C(例如烯),而getDisplayLanguage()是该方法用于获得可读名称(如英文)。
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").
那么,如何来,下面的code在Android中:
So how comes that the following code in Android:
Locale.getDefault().getLanguage()
返回英语或西班牙语,而不是恩和ES????
returns "English" or "Español" instead of "en" and "es"????
我完全摸不着头脑......
I'm completely puzzled...
推荐答案
我已经想通了。发生这种情况是因为我有previously称为Locale.setDefault(),并通过它反过来我被错误地传递给它的整个语言的名字创造了一个语言环境(我把语言从preference设置,我错误地挑的条目,而不是值)的标签
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).
这就是我所做的:
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"
所以,当我询问了默认的语言环境,它实际上是我创造的语言code我曾错误地设置区域设置为英语。
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:
- 在该生产线(*)的实际工作和实际执行区域设置更改为英语(或西班牙语,当我用西班牙语),也就是setDefault()似乎接受了畸形的语言环境,甚至理解它。但它并没有解决它。
- 请注意我用大写英文时错误地设置了语言环境,但最终它返回英语全部小写。
这篇关于为什么Locale.getDefault()。使用getLanguage()Android中返回的语言code中的显示名称呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!