我正在开发一种支持多种语言的应用程序,因此当用户更改语言时,我们将使用Intent.FLAG_ACTIVITY_CLEAR_TOP
,FLAG_ACTIVITY_CLEAR_TASK
和FLAG_ACTIVITY_NEW_TASK
标志开始启动活动,然后完成语言活动。
Intent mIntent = new Intent(this, SplashActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
finish();
我们创建了包装器类来包装应用程序资源
class AppResources(private val context: Context): IAppResources {
override fun getString(resId: Int): String {
Logger.i("Current locale language is: ${context.resources.configuration.locale.language}")
return context.getString(resId)
}
override fun getStringArray(resId: Int): Array<String> {
Logger.i("Current locale language is: ${context.resources.configuration.locale.language}")
return context.resources.getStringArray(resId)
}
}
然后我们像这样使用Koin注入该类
factory<IAppResources> {
Logger.i("Current androidContext language is: ${androidContext().resources.configuration.locale.language}")
AppResources(androidContext())
}
问题是,当我们从资源中获取字符串值时,由于Koin已经从先前的android context
和AppResources
类开始使用旧的context
初始化了,所以我们错误地进行了本地化。因此,有任何解决此问题的建议。
koin中的androidContext()
通常是Application
Context
。
我相信您为多语言支持提供的配置替代代码仅适用于Activity
。