如何获取Android中的当前语言?

How to get the current language in Android?

在我的 Android phone 语言设置中,我将语言设置为 English(United Kingdowm).

我使用以下代码获取语言:

Log.d(TAG,"getDisplayLanguage = " + Locale.getDefault().getDisplayLanguage());
Log.d(TAG,"getLanguage = " + Locale.getDefault().getLanguage());
Log.d(TAG,"Resources.getLanguage = " + Resources.getSystem().getConfiguration().locale.getLanguage());
Log.d(TAG,"getResources.getLanguage = " + getResources().getConfiguration().locale);

日志显示如下:

getDisplayLanguage = English
getLanguage = en
Resources.getLanguage = en
getResources.getLanguage = en_GB

没有显示Local.UK

我是不是遗漏了什么?

根据Is en_UK an illegal locale?

中的回答

正确的国家代码是en_GB。语言环境使用 ISO 3166-1 作为国家/地区代码。维基百科的文章包括:

The codes are chosen, according to the ISO 3166/MA, "to reflect the significant, unique component of the country name in order to allow a visual association between country name and country code".[7] For this reason, common components of country names like "Republic", "Kingdom", "United", "Federal" or "Democratic" are normally not used for deriving the code elements. As a consequence, for example, the United Kingdom is officially assigned the alpha-2 code GB rather than UK, based on its official name "United Kingdom of Great Britain and Northern Ireland" (although UK is reserved on the request of the United Kingdom).

那不是我的答案,这是来自链接 post 的答案。以防万一链接的答案有时不可用。

如果您在代码中检查 Locale.UK,则它是 en_GB

http://www.localeplanet.com/icu/en-GB/

它不会 return 像你尝试进入日志按摩那样。 日志消息将只打印字符串值。但是 Locale 是 class 而 UK 是 (public static final Locale UK = null;) class 中的一个变量。 return 您在日志消息中获得的相同字符串值。