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

Link copied to clipboard
fun getLanguage(context: Context): String?
Link copied to clipboard
Link copied to clipboard
fun getLocalizedString(context: Context, resourceId: Int): String?
fun getLocalizedString(context: Context, resourceId: Int, vararg formatArgs: Any): String?
Link copied to clipboard
fun getLocalizedText(context: Context, resourceId: Int): String?
Link copied to clipboard
fun onAttach(context: Context): Context
fun onAttach(context: Context, defaultLanguage: String): Context
Link copied to clipboard
fun setLocale(context: Context, language: String?): Context
Link copied to clipboard
fun updateConfigurationIfSupported(context: Context, config: Configuration, defaultLanguage: String): Configuration?

override fun attachBaseContext(newBase: Context?) { super.attachBaseContext(newBase) val config = Configuration() applyOverrideConfiguration(config) }