LocaleHelper
In your application level implementation class MainApplication : Application() { override fun attachBaseContext(base: Context) { super.attachBaseContext(LocaleHelper.onAttach(base, "en")) }
Call this function when you need to set the locale LocaleHelper.setLocale like LocaleHelper.setLocale(context, "en") Make sure to re-create the activity once you call this function, preferably use a base(abstract) activity that has the LocaleHelper.onAttach so that all of your functions inherit from it, to ease your problems.
In your activity override this function if using appcompat 1.1.0 override fun attachBaseContext(newBase: Context?) { super.attachBaseContext(newBase?.let { LocaleHelper.onAttach(it) })
If you're using Appcompat delegate or setting the locale on pre < API 24 devices make sure to use this override in activity since they haven't fixed the bug in ages
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) { if (overrideConfiguration != null) { val uiMode = overrideConfiguration.uiMode overrideConfiguration.setTo(baseContext.resources.configuration) overrideConfiguration.uiMode = uiMode } super.applyOverrideConfiguration(overrideConfiguration) }
<------------------>
if using appcompat +1.2.0 override fun attachBaseContext(newBase: Context?) { super.attachBaseContext(newBase) val config = Configuration() applyOverrideConfiguration(config) }
override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) { super.applyOverrideConfiguration(overrideConfiguration?.let { LocaleHelper.updateConfigurationIfSupported(this, it, DEFAULT_LANGUAGE) }) }
}
Functions
override fun attachBaseContext(newBase: Context?) { super.attachBaseContext(newBase) val config = Configuration() applyOverrideConfiguration(config) }