如何在Android设备中选择当前语言?
当前回答
下面是获取设备国家的代码。兼容所有版本的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;
}
其他回答
Locale.getDefault().getDisplayLanguage()
会给你语言的书面名称,例如,英语,荷兰语,法语
Locale.getDefault().getLanguage()
会给你语言代码,例如:en, nl, fr
两种方法都返回String
您可以使用此代码找出键盘电流
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();
我的解是这样的
@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());
}
显示——>和
其他人对设备语言给出了很好的答案,
如果您希望使用应用程序语言,最简单的方法是在strings.xml文件中添加一个app_lang键,并为每个语言指定lang。
这样,如果你的应用程序的默认语言与设备语言不同,你可以选择将其作为服务的参数发送。
如Locale引用中所述,获取语言的最佳方法是:
Locale.getDefault().getLanguage()
该方法根据ISO 639-1标准返回带有语言id的字符串
推荐文章
- 本土化和国际化有什么区别?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图