如何在Android设备中选择当前语言?
当前回答
如果你想检查当前语言,请使用@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())
其他回答
你可以使用 .getLanguage .setLanguage (Locale.forLanguageTag (Locale.getDefault () ())); 这很好
对我有用的是:
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());
请注意,这里的大多数答案提供了应用程序的语言。在某些情况下,这个应用程序可以拥有/设置与设备不同的语言。
获取实际的设备语言(是的,如果用户在设置中添加了多种语言,它将返回所有的语言!)
科特林:
// 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
public void GetDefaultLanguage( ) {
try {
String langue = Locale.getDefault().toString(); // ---> en_US
/*
Log.i("TAG", Locale.getDefault().getLanguage() ); // ---> en
Log.i("TAG", Locale.getDefault().getISO3Language() ); // ---> eng
Log.i("TAG", Locale.getDefault().getCountry() ); // ---> US
Log.i("TAG", Locale.getDefault().getISO3Country() ); // ---> USA
Log.i("TAG", Locale.getDefault().getDisplayCountry() ); // ---> United States
Log.i("TAG", Locale.getDefault().getDisplayName() ); // ---> English (United States)
Log.i("TAG", Locale.getDefault().toString() ); // ---> en_US
Log.i("TAG", Locale.getDefault().getDisplayLanguage() ); //---> English
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
langue = Locale.getDefault().toLanguageTag(); // ---> en-US
url_Api = getUrlMicrosoftLearn(langue);
Log.i("TAG", url_Api );
Log.i("TAG", langue );
}else{
langue = langue.replace("_","-"); // ---> en-US
url_Api = getUrlMicrosoftLearn(langue);
Log.i("TAG", url_Api );
Log.i("TAG", langue );
}
}catch (Exception ex) {
Log.i("TAG", "Exception:GetDefaultLanguage()", ex);
}
}
public String getUrlMicrosoftLearn(String langue) {
return "https://learn.microsoft.com/"+langue+"/learn";
}
其他人对设备语言给出了很好的答案,
如果您希望使用应用程序语言,最简单的方法是在strings.xml文件中添加一个app_lang键,并为每个语言指定lang。
这样,如果你的应用程序的默认语言与设备语言不同,你可以选择将其作为服务的参数发送。
推荐文章
- 本土化和国际化有什么区别?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图