Change app language programmatically in Android

前端 未结 30 3748
面向向阳花
面向向阳花 2020-11-21 04:34

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific lan

30条回答
  •  耶瑟儿~
    2020-11-21 05:11

    https://stackoverflow.com/a/48531811/9609776

    This is ok but do not split the updateResources into different versions, just use the solution below (kotlin). Key is in "Configuration(context.resources.configuration)" it makes deep copy.

    100% solution for API 21+. I have not tested for lower ones, but should work.

    private fun updateResources(context: Context, language: String): Context {
        return Configuration(context.resources.configuration).run {
            Locale.setDefault(Locale(language).also { locale ->
                setLocale(locale)
            }).let {
                context.createConfigurationContext(this)
            }
        }
    }
    

提交回复
热议问题