某些设备中的 Android 阿拉伯语支持问题

sav*_*ion 5 android arabic samsung-mobile android-resources

我正在努力支持阿拉伯语。当我使用以下代码更改应用程序中的语言时(请参阅下文),在大多数设备中它工作正常,但在某些设备(例如Galaxy S7, Galaxy S8, LG G5UI 转换为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)
    }
Run Code Online (Sandbox Code Playgroud)

Hal*_*CAN 1

在您的活动中,覆盖 AttachBaseContext 函数

override fun attachBaseContext(base: Context) {
    super.attachBaseContext(updateBaseContextLocale(base))
}
Run Code Online (Sandbox Code Playgroud)

updateBaseContextLocale 函数如下;

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

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)
}
Run Code Online (Sandbox Code Playgroud)

我希望这个答案可以解决您的问题:)