androidandroid-jetpack-composeandroid-appcompat

Per-app language is not working on Android 10


I am using per-app languages it is working on other devices but on android 10 it is not working.

these are the functions to get and set language. and when user selects other language (french in my case) I am recreating the activity.

fun Context.setLanguage(language: String) {
    if (SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        this.getSystemService(LocaleManager::class.java).applicationLocales =
            LocaleList.forLanguageTags(language)
    } else {
        AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(language))
    }
}

val Context.language: String?
    get() {
        return try {
            if (SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                this.getSystemService(LocaleManager::class.java).applicationLocales[0]?.language
            } else {
                AppCompatDelegate.getApplicationLocales()[0]?.language
            }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

starting activity again

fun Activity.startAgain() {
    finish()
    startActivity(Intent(this, javaClass))
}

and here I am setting language in UI.

itemsIndexed(viewModel.languagesDto) { index, language ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .clip(RectangleShape)
                    .clickable(role = Role.Button) {
                        if (viewModel.selectedLanguageIndex != index) {
                            viewModel.selectedLanguageIndex = index
                            val lang = if (index == 0) ApiConstants.Language.EN
                            else ApiConstants.Language.FR
                            AppPrefs(context).languageId = if (index == 0) "1" else "2"
                            context.setLanguage(lang)
                            onBack()
                        }
                    }
                    .padding(spacing.medium),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.spacedBy(spacing.medium)
            ) {
                Icon(
                    painter = painterResource(id = language.id.getLanguageIcon()),
                    contentDescription = null,
                    tint = if (viewModel.selectedLanguageIndex == index) Color.Cameron else Color.Black
                )

                Text(
                    text = language.name,
                    style = MaterialTheme.typography.body1.copy(
                        fontWeight = FontWeight.Bold,
                        color = if (viewModel.selectedLanguageIndex == index) Color.Cameron else Color.Black
                    )
                )
            }
        }

in onBack I am calling activity.startAgain() function.

Thank you.


Solution

  • after spending some time I figured out that it wasn't an Android 10-specific issue rather it was an issue related to the bundle. When the bundle was uploaded to the console, google play was splitting the French resources (in my case) as per the user's phone language, assuming that the user doesn't need the French language if the phone's language is English. adding the following language block solved the issue.

    bundle {
            language {
                // Specifies that the app bundle should not support
                // configuration APKs for language resources. These
                // resources are instead packaged with each base and
                // dynamic feature APK.
                enableSplit = false
            }
        }