问题描述
我遇到了有关更改Locale
的怪异行为.每个Android API levels
的一切都正常运行.尝试更改我的应用程序的Locale
,但它不再起作用.设置新的Locale -> AttachBaseContext -> Recreate Activity
时,一切似乎都可以正常工作,但最终我只能看到English
中的字符串.
I am experiencing a weird behavior about changing Locale
. Everything was working fine for every Android API levels
.Trying to change the Locale
of my app but it is not working anymore.Everything seems to works fine when setting new Locale -> AttachBaseContext -> Recreate Activity
, but in the ends i see only strings from English
.
您给出什么建议,或者最新的Android Studio 3.5 Canary 9
可能有问题?
What recommendations do you give or this may be something wrong with latest Android Studio 3.5 Canary 9
?
这是我的自定义上下文包装器:
This is my Custom Context Wrapper :
class MyContextWrapper(base: Context) : ContextWrapper(base) {
companion object {
@TargetApi(Build.VERSION_CODES.N)
fun wrap(contextt: Context, newLocale: Locale): ContextWrapper {
var context = contextt
val res = context.resources
val configuration = res.configuration
when {
VersionUtils.isAfter24 -> {
configuration.setLocale(newLocale)
val localeList = LocaleList(newLocale)
LocaleList.setDefault(localeList)
configuration.locales = localeList
context = context.createConfigurationContext(configuration)
}
VersionUtils.isAfter17 -> {
configuration.setLocale(newLocale)
context = context.createConfigurationContext(configuration)
}
else -> {
@Suppress("DEPRECATION")
configuration.locale = newLocale
@Suppress("DEPRECATION")
res.updateConfiguration(configuration, res.displayMetrics)
}
}
return ContextWrapper(context)
}
}
}
推荐答案
这是AppCompat 1.1.0-alpha03
中的一个错误,最后使用最新的alpha AppCompat 1.1.0-alpha04
解决了该问题.
It was a bug in AppCompat 1.1.0-alpha03
, finally it is solved with the latest alpha AppCompat 1.1.0-alpha04
.
我花了三天半的时间来了解由于许多重构而出了什么问题,然后才注意到这种行为.
It takes me 3 days and half to understand what's was going wrong because of to many refactoring before notice this behavior.
当您early alpha tester
时会发生这种情况!
This happen when you are early alpha tester
!
作为建议,因为这发生在我之前(不同的错误),所以请确保在继续改进和重构应用程序之前,先测试要更新到另一个Alpha时正在使用的所有功能. :):)
As suggestion because this happened before to me (different bug), just be sure to test all the features that you are using when updating to another alpha before continuing to improve and refactor the app. :) :)
这篇关于Android-Locale Changer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!