如何在Android设备中选择当前语言?
当前回答
如果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
其他回答
请注意,这里的大多数答案提供了应用程序的语言。在某些情况下,这个应用程序可以拥有/设置与设备不同的语言。
获取实际的设备语言(是的,如果用户在设置中添加了多种语言,它将返回所有的语言!)
科特林:
// Will return something like ["en_US", "de_DE"]
val deviceLanguages: LocaleListCompat = ConfigurationCompat.getLocales(Resources.getSystem().configuration)
// Will return the actual language in use, like "en" or "de". The first language in the above code will be the default language
val currentActiveDeviceLanguage = languages.get(0).language
我检查了我的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
对我有用的是:
Resources.getSystem().getConfiguration().locale;
Resources. getsystem()返回一个全局共享资源对象,该对象仅提供对系统资源(不包括应用程序资源)的访问,并且没有为当前屏幕配置(不能使用维度单位,不会根据方向改变等等)。
因为getConfiguration。locale现在已经被弃用,在Android牛轧糖中获取主locale的首选方法是:
Resources.getSystem().getConfiguration().getLocales().get(0);
为了保证与之前的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());
以上答案没有区分简体中文和繁体中文。 Locale.getDefault(). tostring()工作,返回“zh_CN”,“zh_TW”,“en_US”等。
参考:https://developer.android.com/reference/java/util/Locale.html, ISO 639-1是旧的。
再加上约翰·佩尔格里姆的回答
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);
}
and
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 ...
}
推荐文章
- ImageView -有高度匹配宽度?
- 如何确定在android文件的MIME类型?
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图