Set Locale programmatically

后端 未结 14 1437
Happy的楠姐
Happy的楠姐 2020-11-22 05:55

My app supports 3 (soon 4) languages. Since several locales are quite similar I\'d like to give the user the option to change locale in my application, for instance an Itali

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 06:18

    For those who tried everything but not not working. Please check that if you set darkmode with AppCompatDelegate.setDefaultNightMode and the system is not dark, then Configuration.setLocale will not work above Andorid 7.0.

    Add this code in your every activity to solve this issue:

    override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
      if (overrideConfiguration != null) {
        val uiMode = overrideConfiguration.uiMode
        overrideConfiguration.setTo(baseContext.resources.configuration)
        overrideConfiguration.uiMode = uiMode
      }
      super.applyOverrideConfiguration(overrideConfiguration)
    }
    

提交回复
热议问题