如何更改语言环境?不适用于某些设备
How to change Locale? Not working on some Devices
我正在编写支持两种语言的应用程序,我正在使用更改应用程序区域设置更改语言我的代码:
Locale locale = new Locale("fa");
Locale.setDefault(locale);
Configuration configs = new Configuration();
configs.locale = locale;
getBaseContext().getResources().updateConfiguration(configs, getBaseContext().getResources().getDisplayMetrics());
和
在清单中我已设置 android:supportsRtl="true"
这些代码在许多设备中有效,但在某些设备中无效。例如文本不是翻译而是方向改变。
测试设备:
- 三星 J5Pro 2018 (android = 7.1):成功
- 像素 2 API 26:有效
- 三星 J7 2017 (android = 7):成功
- Nexus 5 (android = 6):未使用
- 三星 Galaxy G531(android < lollipop):未使用
fun changeLang(context: Context, lang_code: String): ContextWrapper {
defaultLanguage = lang_code
var context = context
val sysLocale: Locale
val rs = context.resources
val config = rs.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = config.locales.get(0)
} else {
sysLocale = config.locale
}
if (lang_code != "" && !sysLocale.language.equals(lang_code)) {
val locale = Locale(lang_code)
Locale.setDefault(locale)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale)
} else {
config.locale = locale
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config)
} else {
context.resources.updateConfiguration(config, context.resources.displayMetrics)
}
}
return ContextWrapper(context)
}
把这个放在每个 activity 你想改变语言
override fun attachBaseContext(newBase: Context) {
val lang_code = Shared.defaultLanguage //load it from SharedPref
val context = Shared.changeLang(newBase, lang_code)
super.attachBaseContext(context)
}
你可以试试这段代码,它在 Kotlin 中,有关更多信息,你可以查看
我认为这是 API 的事情。
或者试试这个方法,我在很多应用中都用过,而且很有效
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
fun fixUpLocale(ctx: Context, newLocale: Locale) {
val res = ctx.resources
val config = res.configuration
val curLocale = getLocale(config)
if (curLocale != newLocale) {
Locale.setDefault(newLocale)
val conf = Configuration(config)
conf.setLocale(newLocale)
res.updateConfiguration(conf, res.displayMetrics);
}
}
fun getLocale(config: Configuration): Locale {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return config.locales[0]
} else {
//noinspection deprecation
return config.locale;
}
}
我找到了我的解决方案,我的问题是我在区域设置中插入了 "fa"
,我的字符串值目录名称是 values-fa-rlIR
,所以名称不同所以没有用,我想知道为什么它在某些设备上工作!
我只是将字符串值目录名称从 values-fa-rlIR
更改为 values-fa
,并且运行良好。
有同样的问题。
Android Studio 将语言文件夹命名为 values-xx-rXX。 Android 6.0 及更低版本无法处理此问题,因此您必须将文件夹名称(我直接在 Windows Explorer 中完成)重命名为 values-de。
现在它适用于所有设备
我正在编写支持两种语言的应用程序,我正在使用更改应用程序区域设置更改语言我的代码:
Locale locale = new Locale("fa");
Locale.setDefault(locale);
Configuration configs = new Configuration();
configs.locale = locale;
getBaseContext().getResources().updateConfiguration(configs, getBaseContext().getResources().getDisplayMetrics());
和
在清单中我已设置 android:supportsRtl="true"
这些代码在许多设备中有效,但在某些设备中无效。例如文本不是翻译而是方向改变。
测试设备:
- 三星 J5Pro 2018 (android = 7.1):成功
- 像素 2 API 26:有效
- 三星 J7 2017 (android = 7):成功
- Nexus 5 (android = 6):未使用
- 三星 Galaxy G531(android < lollipop):未使用
fun changeLang(context: Context, lang_code: String): ContextWrapper {
defaultLanguage = lang_code
var context = context
val sysLocale: Locale
val rs = context.resources
val config = rs.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = config.locales.get(0)
} else {
sysLocale = config.locale
}
if (lang_code != "" && !sysLocale.language.equals(lang_code)) {
val locale = Locale(lang_code)
Locale.setDefault(locale)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale)
} else {
config.locale = locale
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config)
} else {
context.resources.updateConfiguration(config, context.resources.displayMetrics)
}
}
return ContextWrapper(context)
}
把这个放在每个 activity 你想改变语言
override fun attachBaseContext(newBase: Context) {
val lang_code = Shared.defaultLanguage //load it from SharedPref
val context = Shared.changeLang(newBase, lang_code)
super.attachBaseContext(context)
}
你可以试试这段代码,它在 Kotlin 中,有关更多信息,你可以查看
我认为这是 API 的事情。
或者试试这个方法,我在很多应用中都用过,而且很有效
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
fun fixUpLocale(ctx: Context, newLocale: Locale) {
val res = ctx.resources
val config = res.configuration
val curLocale = getLocale(config)
if (curLocale != newLocale) {
Locale.setDefault(newLocale)
val conf = Configuration(config)
conf.setLocale(newLocale)
res.updateConfiguration(conf, res.displayMetrics);
}
}
fun getLocale(config: Configuration): Locale {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return config.locales[0]
} else {
//noinspection deprecation
return config.locale;
}
}
我找到了我的解决方案,我的问题是我在区域设置中插入了 "fa"
,我的字符串值目录名称是 values-fa-rlIR
,所以名称不同所以没有用,我想知道为什么它在某些设备上工作!
我只是将字符串值目录名称从 values-fa-rlIR
更改为 values-fa
,并且运行良好。
有同样的问题。 Android Studio 将语言文件夹命名为 values-xx-rXX。 Android 6.0 及更低版本无法处理此问题,因此您必须将文件夹名称(我直接在 Windows Explorer 中完成)重命名为 values-de。 现在它适用于所有设备