Facing issue on changing App Language in Android 13 & 14

394 Views Asked by At

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.

3

There are 3 best solutions below

0
Taein On

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.

1
Naresh Prajapati On

The provided code snippet addresses a potential cause of missing language resources in your app built using Android App Bundles (AAB) for Play Store publishing. Here's a breakdown of the code and its implications:

Code Explanation:

Gradle

android {
  //... removed for brevity
  bundle {
     language {
       enableSplit = false
     }
   }
}
0
Senocico Stelian On

For the API level 33 and above you should use: AppCompatDelegate.setApplicationLocales(localeListCompat)

Full example:

    private fun updateLocaleConfiguration(configuration: Configuration, newLocale: Locale): Configuration {
        Locale.setDefault(newLocale)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(newLocale)
            val localeList = LocaleList(newLocale)
            LocaleList.setDefault(localeList)
            configuration.setLocales(localeList)

            // If Device is running Android 14
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.TIRAMISU) {
                val android14OrAboveLocale = LocaleListCompat.forLanguageTags(newLocale.language)
                AppCompatDelegate.setApplicationLocales(android14OrAboveLocale)
            }
        } else {
            configuration.setLocale(newLocale)
        }
        return configuration
    }