如何在Android设备中选择当前语言?
当前回答
如Locale引用中所述,获取语言的最佳方法是:
Locale.getDefault().getLanguage()
该方法根据ISO 639-1标准返回带有语言id的字符串
其他回答
我的解是这样的
@SuppressWarnings("deprecation")
public String getCurrentLocale2() {
return Resources.getSystem().getConfiguration().locale.getLanguage();
}
@TargetApi(Build.VERSION_CODES.N)
public Locale getCurrentLocale() {
getResources();
return Resources.getSystem().getConfiguration().getLocales().get(0);
}
然后
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.e("Locale", getCurrentLocale().getLanguage());
} else {
Log.e("Locale", getCurrentLocale2().toString());
}
显示——>和
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
// your code here
}
如果你使用喷气背包撰写
在可组合功能方面
Context.applicationContext.resources.configuration.locales.get(0).language
Locale.getDefault().getDisplayLanguage()
会给你语言的书面名称,例如,英语,荷兰语,法语
Locale.getDefault().getLanguage()
会给你语言代码,例如:en, nl, fr
两种方法都返回String
如果你想检查当前语言,请使用@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())
推荐文章
- Android是否保留。apk文件?如果有,在哪里?
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?
- 如何确定在android文件的MIME类型?
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调