如何在Android设备中选择当前语言?
当前回答
其他人对设备语言给出了很好的答案,
如果您希望使用应用程序语言,最简单的方法是在strings.xml文件中添加一个app_lang键,并为每个语言指定lang。
这样,如果你的应用程序的默认语言与设备语言不同,你可以选择将其作为服务的参数发送。
其他回答
为了节省其他人的时间和/或混乱,我想分享我已经尝试了上面Johan Pelgrim提出的两个替代方案,在我的设备上它们是等效的——无论默认位置是否改变。
所以我的设备的默认设置是英语(英国),在这种状态下,约翰的回答中的两个字符串都给出了相同的结果。如果我然后在电话设置中更改区域设置(说到italiano(意大利))并重新运行,那么在Johan的回答中两个字符串都将区域设置为italiano(意大利)。
因此,我认为Johan最初的帖子是正确的,而gregm的评论是不正确的。
您可以使用此代码找出键盘电流
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();
您可以从当前区域“提取”语言。你可以通过标准的Java API或者使用Android Context来提取语言环境。例如,下面这两行是等价的:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
我的解是这样的
@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());
}
显示——>和
再加上约翰·佩尔格里姆的回答
context.getResources().getConfiguration().locale
Locale.getDefault()
是等价的,因为android.text.format.DateFormat类可以互换使用这两者。
private static String zeroPad(int inValue, int inMinDigits) {
return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}
and
public static boolean is24HourFormat(Context context) {
String value = Settings.System.getString(context.getContentResolver(),
Settings.System.TIME_12_24);
if (value == null) {
Locale locale = context.getResources().getConfiguration().locale;
// ... snip the rest ...
}
推荐文章
- 本土化和国际化有什么区别?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图