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...
Use
and it will work just fine even though I would consider your observed behaviour a bug worth reporting..
Android is returning the readable names instead of the codes.
Locale.getDefault() has the string. So if you call any prints or Logs on that it'll work... meaning Locale.getDefault().toString() has your locale code.
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:
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:
I don't know why this issue appears, but another standard for languages is the ISO3 code. You can call
Locale.getDefault().getISO3Language()
and it should return "eng" or "esp".