本地化有时会重置为系统区域设置
Localization sometimes reset to system locale
我有一个 activity 应用程序,语言设置基本上按预期工作。我通过文本的资源 ID 在 onCreateView()
中设置每个视图的文本。但有时,当我进入我的应用程序时,语言是系统默认语言而不是所选语言。当我从深度 link(小部件或通知)导航到应用程序时,也会发生同样的情况。当我导航到另一个片段和 return 时,一切都会在正确的语言环境下变得正确。
我尝试调试我的应用程序以找出原因。我根据系统默认值而不是选择的语言获得的每个按资源 ID 的文本。当我改为包装我的片段上下文然后获取文本时,我得到了正确的结果。
这是我设置语言的方式:
private fun changeLang(lang: String, country: String) {
activity?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
updateResources(it, lang, country)
}
updateResourcesLegacy(it, lang, country)
}
}
@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String, country: String) {
val locale = Locale(language, country)
Locale.setDefault(locale)
val resources: Resources = context.resources
val configuration: Configuration = resources.configuration
configuration.setLocale(locale)
context.createConfigurationContext(configuration)
}
private fun updateResourcesLegacy(context: Context, language: String, country: String) {
val locale = Locale(language, country)
Locale.setDefault(locale)
val resources: Resources = context.resources
val configuration: Configuration = resources.configuration
configuration.locale = locale
resources.updateConfiguration(configuration, resources.displayMetrics)
}
在我的 activity 中,我覆盖了以下方法:
override fun attachBaseContext(newBase: Context?) {
newBase?.let {
val pref = Preference(PreferenceHelper.customPrefs(it))
super.attachBaseContext(ContextWrapper.wrap(newBase, pref.language, pref.country))
}
}
class ContextWrapper(base: Context?) : android.content.ContextWrapper(base) {
companion object {
fun wrap(context: Context?, lang: String? = "uz", country: String? = ""): ContextWrapper =
wrap(context, Locale(lang, country))
fun wrap(context: Context?, newLocale: Locale): ContextWrapper {
var localizedContext = context
val res = localizedContext?.resources
val configuration = res?.configuration
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
configuration?.setLocale(newLocale)
val localeList = LocaleList(newLocale)
LocaleList.setDefault(localeList)
configuration?.setLocales(localeList)
localizedContext = localizedContext?.createConfigurationContext(configuration!!)
}
else -> {
configuration?.setLocale(newLocale)
localizedContext = localizedContext?.createConfigurationContext(configuration!!)
}
}
res?.updateConfiguration(configuration, res.displayMetrics)
return ContextWrapper(localizedContext)
}
}
}
尝试同时写入:
configuration.setLocale(locale)
configuration.locale = locale
在 ContextWrapper
class 和 updateResources
函数中试试这个
原因是没有为应用程序上下文设置语言。应用程序资源没有得到更新。我在我的应用程序 class.
的 attachBaseContext 方法上应用了语言环境
我有一个 activity 应用程序,语言设置基本上按预期工作。我通过文本的资源 ID 在 onCreateView()
中设置每个视图的文本。但有时,当我进入我的应用程序时,语言是系统默认语言而不是所选语言。当我从深度 link(小部件或通知)导航到应用程序时,也会发生同样的情况。当我导航到另一个片段和 return 时,一切都会在正确的语言环境下变得正确。
我尝试调试我的应用程序以找出原因。我根据系统默认值而不是选择的语言获得的每个按资源 ID 的文本。当我改为包装我的片段上下文然后获取文本时,我得到了正确的结果。
这是我设置语言的方式:
private fun changeLang(lang: String, country: String) {
activity?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
updateResources(it, lang, country)
}
updateResourcesLegacy(it, lang, country)
}
}
@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String, country: String) {
val locale = Locale(language, country)
Locale.setDefault(locale)
val resources: Resources = context.resources
val configuration: Configuration = resources.configuration
configuration.setLocale(locale)
context.createConfigurationContext(configuration)
}
private fun updateResourcesLegacy(context: Context, language: String, country: String) {
val locale = Locale(language, country)
Locale.setDefault(locale)
val resources: Resources = context.resources
val configuration: Configuration = resources.configuration
configuration.locale = locale
resources.updateConfiguration(configuration, resources.displayMetrics)
}
在我的 activity 中,我覆盖了以下方法:
override fun attachBaseContext(newBase: Context?) {
newBase?.let {
val pref = Preference(PreferenceHelper.customPrefs(it))
super.attachBaseContext(ContextWrapper.wrap(newBase, pref.language, pref.country))
}
}
class ContextWrapper(base: Context?) : android.content.ContextWrapper(base) {
companion object {
fun wrap(context: Context?, lang: String? = "uz", country: String? = ""): ContextWrapper =
wrap(context, Locale(lang, country))
fun wrap(context: Context?, newLocale: Locale): ContextWrapper {
var localizedContext = context
val res = localizedContext?.resources
val configuration = res?.configuration
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
configuration?.setLocale(newLocale)
val localeList = LocaleList(newLocale)
LocaleList.setDefault(localeList)
configuration?.setLocales(localeList)
localizedContext = localizedContext?.createConfigurationContext(configuration!!)
}
else -> {
configuration?.setLocale(newLocale)
localizedContext = localizedContext?.createConfigurationContext(configuration!!)
}
}
res?.updateConfiguration(configuration, res.displayMetrics)
return ContextWrapper(localizedContext)
}
}
}
尝试同时写入:
configuration.setLocale(locale)
configuration.locale = locale
在 ContextWrapper
class 和 updateResources
函数中试试这个
原因是没有为应用程序上下文设置语言。应用程序资源没有得到更新。我在我的应用程序 class.
的 attachBaseContext 方法上应用了语言环境