当“添加语言”部分中不存在语言时,Android 运行时语言更改不起作用

Phi*_*ais 5 android localization internationalization kotlin

我正在尝试使用以下代码在本地更改我的应用程序语言。如果手机设置的添加语言中有英语和法语,然后进入我的应用更改语言,则语言更改成功,但如果我从手机设置的添加语言中删除语言,则改变不起作用。从我在网上看到的示例来看,应该可以更改它,而无需在手机设置的“添加语言”中添加任何其他语言。我不确定我做错了什么,有没有一种方法可以启用语言更改,而无需在手机设置中添加语言?

任何指向某些文档的链接也将非常感激。

这是我创建的用于启用语言切换的 LocalUtil 对象:

object LocalUtil {

    fun applyLanguageContext(context: Context, locale: Locale?): Context {

        if (locale == null) return context
        if (locale == getLocale(context.resources.configuration)) return context
        return try {
            setupLocale(locale)
            val resources = context.resources
            val configuration = getOverridingConfig(locale, resources)
            updateResources(context, resources, configuration)
            context.createConfigurationContext(configuration)
        } catch (e: Exception) {
            e.printStackTrace()
            context
        }
    }

    private fun updateResources(
        context: Context,
        resources: Resources,
        config: Configuration
    ) {
        if (context.applicationContext !== context) {
            resources.updateConfiguration(config, resources.displayMetrics)
        }
    }

    private fun setupLocale(locale: Locale) {
        Locale.setDefault(locale)
        LocaleList.setDefault(LocaleList(locale))
    }

    private fun getOverridingConfig(locale: Locale, resources: Resources): Configuration {
        val configuration = resources.configuration
        configuration.setLocales(LocaleList(locale))
        return configuration
    }

    private fun getLocale(configuration: Configuration): Locale {
        return configuration.locales.get(0)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是包含 LANGUAGE 伴随对象变量的 Application() 类

class MyApp: Application() {

    override fun getApplicationContext(): Context {
        val context = super.getApplicationContext()
        return LocalUtil.applyLanguageContext(context, Locale(LANGUAGE))
    }

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LocalUtil.applyLanguageContext(newBase, Locale(LANGUAGE)))
    }

    companion object {
        var LANGUAGE = "en"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是MainActivity.kt,带有一个按钮,可以通过以下按钮在英语“en”和法语“fr”之间切换changeLangBtn: Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun setChangeLangBtn(view: View) {
        val changeLangBtn: Button = findViewById(R.id.change_lang_btn)
        changeLangBtn.setOnClickListener {
            if (MyApp.LANGUAGE == "en") MyApp.LANGUAGE = "fr" else MyApp.LANGUAGE = "en"
            reloadActivity()
        }
    }

    fun reloadActivity() {
         val intent = Intent(this, javaClass).apply {
             addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
             addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
         }

         startActivity(intent)
         recreate()
    }

    override fun getBaseContext(): Context {
        return LocalUtil.applyLanguageContext(super.getBaseContext(), Locale(MyApp.LANGUAGE))
    }

    override fun getApplicationContext(): Context {
        val context = super.getApplicationContext()
        return LocalUtil.applyLanguageContext(context, Locale(MyApp.LANGUAGE))
    }

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LocalUtil.applyLanguageContext(newBase, Locale(MyApp.LANGUAGE)))
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

来自此文档: https ://developer.android.com/guide/playcore/feature-delivery/on-demand#lang_resources

如果您使用应用程序捆绑包,则应用程序将仅使用必要的资源运行,因此不会下载默认语言以外的语言资源,除非它们存在于用户的手机设置中。要在使用应用程序包时启用其他语言,您应该实现 Play 功能交付库。按照文档中的步骤来实现这一点!