“应用”活动和模块活动语言不同

问题描述

我的项目有模块并且有活动。当我从“应用程序”调用它时,它会以不同的语言环境打开。但是当我在“app”中更改语言时,我会更改 applicationContext 配置并且它适用于“app”活动但不适用于模块活动。我试图从模块活动的 resource.configuration 获取语言环境,它与“应用程序”活动语言不同。模块有自己的资源,带有用于语言的 string.xml

//in module activity
Locale.getDefault().language //uz
resources.configuration.locale.language //ru_RU

我如何在“应用”活动中更改语言:

//after recreate()

override fun attachBaseContext(newBase: Context?) {
        newBase?.applicationContext?.let {
            super.attachBaseContext(updateBaseContextLocale(it))
        }
    }

private fun updateBaseContextLocale(context: Context): Context? {

        val language: String =
           SettingsRepository.getInstance(context)
                .getLocalization()// Helper method to get saved language from SharedPreferences
        val locale = Locale(language)
        Locale.setDefault(locale)
        return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            updateResourcesLocale(context,locale)
        } else updateResourcesLocaleLegacy(context,locale)
    }

@TargetApi(Build.VERSION_CODES.N_MR1)
    fun updateResourcesLocale(context: Context,locale: Locale): Context {
        val conf = Configuration(context.resources.configuration)
        conf.setLocale(locale)
        return context.createConfigurationContext(conf)
    }

    @SuppressWarnings("deprecation")
    fun updateResourcesLocaleLegacy(context: Context,locale: Locale): Context {
        val resource = context.resources
        val conf = resource.configuration
        conf.locale = locale
        resource.updateConfiguration(conf,resource.displayMetrics)
        return context
    }

我应该怎么做才能让模块活动语言像“应用程序”活动语言一样?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)