I set my android app local via
fun setSystemLocale(context: Context): Context {
val locale = getCurrentLanguage().locale
Locale.setDefault(locale)
val configuration = Configuration(context.resources.configuration)
configuration.setLocale(locale)
configuration.setLayoutDirection(locale)
context.resources.updateConfiguration(configuration, context.resources.displayMetrics)
return context.createConfigurationContext(configuration)
}
set app local work fine on lower versions(Below Android 13) but, on Android 13 & 14 Sometimes language change partially or sometimes language change does not reflect.
Sometimes language is changed partially in Android 13 & 14.
I could find a solution in android's developer doc.
Here is a sample code.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val appLocale = when (languageCode) {
"firstCode" -> LocaleListCompat.forLanguageTags("en")
"secondCode" -> LocaleListCompat.forLanguageTags("ar")
}
// Call this on the main thread as it may require Activity.restart()
AppCompatDelegate.setApplicationLocales(appLocale)
}
If you want to use this solution, your appcompat version must be 1.6.0 or higher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With