如何在Android设备中选择当前语言?
当前回答
获取设备语言的正确方法如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.getResources().getConfiguration().getLocales().get(0);
} else {
return context.getResources().getConfiguration().locale;
}
希望能有所帮助。
其他回答
我的解是这样的
@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.applicationContext.resources.configuration.locales.get(0).language
再加上约翰·佩尔格里姆的回答
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 ...
}
public class LocalUtils {
private static final String LANGUAGE_CODE_ENGLISH = "en";
// returns application language eg: en || fa ...
public static String getAppLanguage() {
return Locale.getDefault().getLanguage();
}
// returns device language eg: en || fa ...
public static String getDeviceLanguage() {
return ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getLanguage();
}
public static boolean isDeviceEnglish() {
return getDeviceLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
}
public static boolean isAppEnglish() {
return getAppLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
}
}
Log.i("AppLanguage: ", LocalUtils.getAppLanguage());
Log.i("DeviceLanguage: ", LocalUtils.getDeviceLanguage());
Log.i("isDeviceEnglish: ", String.valueOf(LocalUtils.isDeviceEnglish()));
Log.i("isAppEnglish: ", String.valueOf(LocalUtils.isAppEnglish()));
以上答案没有区分简体中文和繁体中文。 Locale.getDefault(). tostring()工作,返回“zh_CN”,“zh_TW”,“en_US”等。
参考:https://developer.android.com/reference/java/util/Locale.html, ISO 639-1是旧的。
推荐文章
- 本土化和国际化有什么区别?
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图