如何在Android设备中选择当前语言?
当前回答
这个解决方案对我很有效。这将返回android设备的语言(不是应用程序的本地语言)
String locale = getApplicationContext().getResources().getConfiguration().locale.getLanguage();
这将返回"en"或"de"或"fr"或任何你的设备语言设置。
其他回答
如果你想要获得你的设备所选择的语言,这可能会帮助你:
Locale.getDefault().getDisplayLanguage();
你可以使用Locale.getDefault().getLanguage();要获得常用的语言代码(例如:“德”、“en”)
如果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
基本的Kotlin答案:
Locale.getDefault().language
您可以从当前区域“提取”语言。你可以通过标准的Java API或者使用Android Context来提取语言环境。例如,下面这两行是等价的:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
如果你想检查当前语言,请使用@Sarpe (@Thorbear)的答案:
val language = ConfigurationCompat.getLocales(Resources.getSystem().configuration)?.get(0)?.language
// Check here the language.
val format = if (language == "ru") "d MMMM yyyy г." else "d MMMM yyyy"
val longDateFormat = SimpleDateFormat(format, Locale.getDefault())
推荐文章
- 我如何在一个片段中访问getSupportFragmentManager() ?
- 调试在哪里。Android Studio中的密钥存储库
- 从资产中读取文件
- 在不活动的地方调用getLayoutInflater()
- 设置Android布局元素的背景颜色
- 错误:'keytool'不能被识别为内部或外部命令、可操作程序或批处理文件
- 在应用程序本身中更改Locale
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML