无法在Kotlin中更改语言环境

问题描述 投票:2回答:1

我有2个字符串文件“ en”和“ es”。每当我在语言环境管理器类中更改语言环境时,它都会成功保存新的语言环境,但不会反映刷新活动的更改。

MyApplication.kt

open class MyApplication : Application() {

    init {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(LocaleManagerMew.setLocale(base))
       // MultiDex.install(base)
    }

    override fun onCreate() {
        super.onCreate()

    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        LocaleManagerMew.setLocale(this)
        Log.d("app", "onConfigurationChanged: " + newConfig.locale.getLanguage())
    }
}

LocaleManagerNew.kt

object LocaleManagerMew {

    val SELECTED_LANGUAGE = "MEW_CURRENT_-- USER_LANGUAGE"
    var mSharedPreference: SharedPref? = null

    var mEnglishFlag = "en"
    var mSpanishFlag = "es"

    fun setLocale(context: Context?): Context {
        return updateResources(context!!, getCurrentLanguage(context)!!)
    }

    inline fun setNewLocale(context: Context, language: String) {

        persistLanguagePreference(context, language)
        updateResources(context, language)
    }

    inline fun getCurrentLanguage(context: Context?): String? {

        var mCurrentLanguage: String?

        if (mSharedPreference == null)
            mSharedPreference = SharedPref(context!!)

        mCurrentLanguage = mSharedPreference!!.getSavedLang()

        return mCurrentLanguage
    }

    fun persistLanguagePreference(context: Context, language: String) {
        if (mSharedPreference == null)
            mSharedPreference = SharedPref(context!!)

        mSharedPreference!!.setSavedLang(language)

    }

    fun updateResources(context: Context, language: String): Context {

        var contextFun = context

        var locale = Locale(language)
        Locale.setDefault(locale)

        var resources = context.resources
        var configuration = Configuration(resources.configuration)

        if (Build.VERSION.SDK_INT >= 17) {
            configuration.setLocale(locale)
            contextFun = context.createConfigurationContext(configuration)
        } else {
            configuration.locale = locale
            resources.updateConfiguration(configuration, resources.getDisplayMetrics())
        }
        return contextFun
    }
}

BaseActivity.kt

override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(LocaleManagerMew.setLocale(base))
    }

MainActivity.kt

 R.id.spanishCL ->  {
                sp.setSavedLang("es")
                var mCurrentLanguage = LocaleManagerMew.getCurrentLanguage([email protected])
                LocaleManagerMew.setNewLocale(this@MainActivity, LocaleManagerMew.mSpanishFlag)
               mContext?.recreate()

            }

这是onClick方法调用,用于将英语语言环境更改为西班牙语。重新创建主动性后,新的语言环境更改不会反映]

android kotlin locale
1个回答
1
投票

尝试以下代码从每个活动attachBaseContext中更新语言。>

    fun setLanguage(context: Context, language: String): ContextWrapper {
        var mContext = context

        val localeLang = language.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        val locale: Locale
        if (localeLang.size > 1)
            locale = Locale(localeLang[0], localeLang[1])
        else
            locale = Locale(localeLang[0])

        val res = mContext.resources
        val configuration = res.configuration

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val localeList = LocaleList(locale)
            LocaleList.setDefault(localeList)
            configuration.locales = localeList
            mContext = mContext.createConfigurationContext(configuration)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale)
            mContext = mContext.createConfigurationContext(configuration)
        } else {
            configuration.locale = locale
            res.updateConfiguration(configuration, res.getDisplayMetrics())
        }

        return ContextWrapper(mContext)
    }

以及Activity

类的attachBaseContext中的实现
//    For Language Changing
override fun attachBaseContext(newBase: Context?) {
    super.attachBaseContext(newBase?.let {
        Common.setLanguage(
            it,
            PreferenceManager.getPref<String>(Constants.LANGUAGE_PREFERENCE).toString()
        )
    })
}

这就是我如何重新启动来自片段的活动

Handler().post {
            val intent = activity?.intent
            intent?.flags =
                Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or
                        Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NO_ANIMATION
            activity?.overridePendingTransition(0, 0)
            activity?.finish()

            activity?.overridePendingTransition(0, 0)
            startActivity(intent)
        }
© www.soinside.com 2019 - 2024. All rights reserved.