如何更改语言环境?在某些设备上不工作

时间:2018-10-24 08:05:53

标签: android locale multilingual

我正在编写支持两种语言的应用程序,并且在这里通过我的代码使用“更改应用程序语言环境”来更改语言:

 Locale locale = new Locale("fa");
            Locale.setDefault(locale);
            Configuration configs = new Configuration();
            configs.locale = locale;
            getBaseContext().getResources().updateConfiguration(configs, getBaseContext().getResources().getDisplayMetrics());

还有

清单中的我设置为android:supportsRtl="true"

这些代码可在许多设备上使用,但在某些设备上无法使用。例如,文字不翻译但方向改变。

经过测试的设备:

  • 三星J5Pro 2018(android = 7.1):工作
  • Pixel 2 API 26:有效
  • 三星J7 2017(android = 7):工作
  • Nexus 5(android = 6):不起作用
  • 三星Galaxy G531(android <棒棒糖):不起作用

4 个答案:

答案 0 :(得分:1)

我找到了解决方案,我的问题是我在语言环境中插入了"fa",我的字符串值目录名称是values-fa-rlIR,所以名称不同,所以无法使用,我想知道为什么会这样在某些设备上工作!

我只是将字符串值目录名称从values-fa-rlIR更改为values-fa,并且运行良好。

答案 1 :(得分:0)

fun changeLang(context: Context, lang_code: String): ContextWrapper {
    defaultLanguage = lang_code
    var context = context
    val sysLocale: Locale

    val rs = context.resources
    val config = rs.configuration

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        sysLocale = config.locales.get(0)
    } else {
        sysLocale = config.locale
    }
    if (lang_code != "" && !sysLocale.language.equals(lang_code)) {
        val locale = Locale(lang_code)
        Locale.setDefault(locale)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocale(locale)
        } else {
            config.locale = locale
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            context = context.createConfigurationContext(config)
        } else {
            context.resources.updateConfiguration(config, context.resources.displayMetrics)
        }
    }

    return ContextWrapper(context)
}

在您要更改语言的每个活动中都输入

   override fun attachBaseContext(newBase: Context) {
    val lang_code = Shared.defaultLanguage //load it from SharedPref
    val context = Shared.changeLang(newBase, lang_code)
    super.attachBaseContext(context)
}

您可以尝试此代码,该代码位于Kotlin中,有关更多信息,请检查Android N change language programmatically

我认为这是API问题。

答案 2 :(得分:0)

或尝试这种方法,我在许多应用程序中使用了它,而且效果很好

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    fun fixUpLocale(ctx: Context, newLocale: Locale) {
        val res = ctx.resources
        val config = res.configuration
        val curLocale = getLocale(config)
        if (curLocale != newLocale) {
            Locale.setDefault(newLocale)
            val conf = Configuration(config)
            conf.setLocale(newLocale)
            res.updateConfiguration(conf, res.displayMetrics);
        }
    }

    fun getLocale(config: Configuration): Locale {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return config.locales[0]
        } else {
            //noinspection deprecation
            return config.locale;
        }
    }

答案 3 :(得分:0)

有同样的问题。 Android Studio将语言文件夹命名为values-xx-rXX。 Android 6.0及更低版本无法处理此问题,因此您必须将文件夹名(我直接在Windows资源管理器中直接将其重命名)为values-de。 现在,它适用于所有设备