每应用语言在Android 10上不起作用

kqhtkvqz  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(182)

我使用的是每应用程序语言,它在其他设备上工作,但在android 10上不工作。
这些是获取和设置语言的函数。当用户选择其他语言(在我的情况下是法语)时,我正在重新创建活动。

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
        }
    }

再次启动活动

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

这里我在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
                    )
                )
            }
        }

onBack中调用activity.startAgain()函数。
谢谢大家。

7eumitmz

7eumitmz1#

花了一些时间后,我发现这不是Android 10特定的问题,而是与捆绑包相关的问题。当捆绑包上传到控制台时,Google Play正在分割法语资源(在我的情况下)根据用户的电话语言,假设如果手机的语言是英语,用户不需要法语。添加下面的language块解决了这个问题。

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
        }
    }

相关问题