我们如何获取Android设备中当前选择的语言?
我们如何获取Android设备中当前选择的语言?
我已经在我的Android 4.1.2设备上检查了Locale方法,并得出以下结果:
Locale.getDefault().getLanguage() ---> en
Locale.getDefault().getISO3Language() ---> eng
Locale.getDefault().getCountry() ---> US
Locale.getDefault().getISO3Country() ---> USA
Locale.getDefault().getDisplayCountry() ---> United States
Locale.getDefault().getDisplayName() ---> English (United States)
Locale.getDefault().toString() ---> en_US
Locale.getDefault().getDisplayLanguage()---> English
Locale.getDefault().toLanguageTag() ---> en-US
Locale.getDefault().getDisplayLanguage()
,它返回 "English"
。 - sports如果您想获取设备选择的语言,这可能对您有所帮助:
Locale.getDefault().getDisplayLanguage();
您可以使用Locale.getDefault().getLanguage();
来获取通常的语言代码(例如 "de","en")
Locale.getDefault().getLanguage();
获取常见的语言代码(例如 "de"、"en")。 - muscardinusLocale.setDefault("ru")
,而系统设置中的语言设置为英语,则方法 Locale.getDefault().getLanguage()
将返回 "ru" 而不是 **"en"**。有没有另一种获取真正的系统区域设置/语言的方法?我在这里找到了未记录的解决方案(https://dev59.com/nHE85IYBdhLWcg3wMAfc#4683532),但是否有更优雅的解决方案? - PrizoffLocale.getDefault().toString()
,这会返回一个完全标识区域设置的字符串,例如"en_US"。 - Tom对我起作用的是:
Resources.getSystem().getConfiguration().locale;
Resources.getSystem()
返回一个全局共享的 Resources 对象,仅提供访问系统资源(没有应用程序资源),并且未配置为当前屏幕(不能使用尺寸单位,不随方向改变等)。
由于 getConfiguration.locale
已经被弃用,在 Android Nougat 中获取主要语言环境的首选方法是:
Resources.getSystem().getConfiguration().getLocales().get(0);
为确保与之前的 Android 版本兼容,可能的解决方案是进行一个简单的检查:
为确保与之前的 Android 版本兼容,可能的解决方案是进行一个简单的检查:
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
}
更新
从支持库26.1.0
开始,您无需检查Android版本,因为它提供了一个方便的向后兼容方法:getLocales()
。
只需调用:
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())
。 - ThorbeargetLocales
是正确的方法。我已经更新了我的答案。 - Sarpe您可以从当前区域设置中“提取”语言。您可以通过标准的Java API或使用Android Context来提取区域设置。例如,以下两行代码是等效的:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
Locale.setDefault()
,即可获得不同的结果。 - arekolek你可以使用这个
boolean isLang = Locale.getDefault().getLanguage().equals("xx");
当 " xx " 是任何语言代码,如 "en", "fr", "sp", "ar"...等等时
如果API级别大于等于24,则使用LocaleList.getDefault().get(0).getLanguage()
,
否则使用Locale.getDefault.getLanguage()
private fun getSystemLocale() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList.getDefault().get(0).language
} else {
Locale.getDefault().language
}
参考资料:https://developer.android.com/guide/topics/resources/multilingual-support
这个解决方案对我有效。它将返回您的Android设备语言(而不是应用程序的本地语言)。
String locale = getApplicationContext().getResources().getConfiguration().locale.getLanguage();
context.getResources().getConfiguration().locale
Locale.getDefault()
这两个表达方式是等价的,因为android.text.format.DateFormat
类可以互换使用,例如:
private static String zeroPad(int inValue, int inMinDigits) {
return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}
并且
public static boolean is24HourFormat(Context context) {
String value = Settings.System.getString(context.getContentResolver(),
Settings.System.TIME_12_24);
if (value == null) {
Locale locale = context.getResources().getConfiguration().locale;
// ... snip the rest ...
}