安卓阿拉伯语在某些设备上的支持问题

问题描述 投票:1回答:1

我正在尝试支持阿拉伯语。当我用下面的代码改变了应用程序中的语言(参考下面的代码),在大多数设备上都能正常工作,但在一些设备上,如 Galaxy S7, Galaxy S8, LG G5 UI转换为 RTL 成功,但阿拉伯语资源没有被加载,应用程序仍然使用了 EN 字符串资源。

大家对这个问题有什么看法吗?谢谢大家的解答。

这是我用来改变语言的代码。

 private fun updateResources(context: Context, locale: Locale): Context {
        Locale.setDefault(locale)
        val res = context.resources
        val config = res.configuration
        config.setLocale(locale)
        config.setLayoutDirection(locale)
        BaseActivity.setLayoutDirection(res.configuration.layoutDirection)
        Session.lang = locale.language

        return context.createConfigurationContext(config)
    }
android android-resources arabic samsung-mobile language
1个回答
1
投票

在你的活动中,覆盖 attachBaseContext 函数。

override fun attachBaseContext(base: Context) {
    super.attachBaseContext(updateBaseContextLocale(base))
}

updateBaseContextLocale函数是这样的。

private fun updateBaseContextLocale(context: Context): Context {
    val languageCode = LanguageUtils.getLanguageCode(LocalSharedPrefs.getLanguage(context))
    return AppUtils.updateLocale(context, languageCode)
}

在AppUtils中updateLocale函数是这样的。

@JvmStatic
fun updateLocale(context: Context, languageCode: String): Context {
    val locale = Locale(languageCode)
    Locale.setDefault(locale)

    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        updateResourcesLocale(context, locale)
    } else {
        updateResourcesLocaleLegacy(context, locale)
    }
}

private fun updateResourcesLocaleLegacy(context: Context, locale: Locale): Context {
    val resources = context.resources
    val configuration = resources.configuration
    configuration.locale = locale
    configuration.setLayoutDirection(locale)
    resources.updateConfiguration(configuration, resources.displayMetrics)
    return context
}

@TargetApi(Build.VERSION_CODES.N)
private fun updateResourcesLocale(context: Context, locale: Locale): Context {
    val configuration = context.resources.configuration
    configuration.setLocale(locale)
    configuration.setLayoutDirection(locale)
    return context.createConfigurationContext(configuration)
}

我希望你的问题可以通过这个答案得到解决:)

© www.soinside.com 2019 - 2024. All rights reserved.