如何在Android设备中选择当前语言?
当前回答
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())
对我有用的是:
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());
我的解是这样的
@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());
}
显示——>和
下面是获取设备国家的代码。兼容所有版本的android甚至奥利奥。
解决方案:如果用户没有sim卡,那么在手机设置或当前语言选择时获取他所使用的国家。
public static String getDeviceCountry(Context context) {
String deviceCountryCode = null;
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(tm != null) {
deviceCountryCode = tm.getNetworkCountryIso();
}
if (deviceCountryCode != null && deviceCountryCode.length() <=3) {
deviceCountryCode = deviceCountryCode.toUpperCase();
}
else {
deviceCountryCode = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getCountry().toUpperCase();
}
// Log.d("countryCode"," : " + deviceCountryCode );
return deviceCountryCode;
}
如果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
推荐文章
- 本土化和国际化有什么区别?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图