如何在Android的语言环境中设置俄语? Android App本地化

时间:2019-03-03 14:17:31

标签: android localization locale

我的应用程序支持3种语言(英语,德语,俄语)。我使用LocaleHelper类进行语言设置,并按如下所示设置语言:

lateinit var alert: AlertDialog
                val options = arrayOf(
                    resources.getString(R.string.en_lang),
                    resources.getString(R.string.de_lang),
                    resources.getString(R.string.ru_lang))
                val dialogBuilder = AlertDialog.Builder(this, R.style.AboutAlertDialogStyle)
                dialogBuilder.setTitle(resources.getString(R.string.app_language))
                    .setSingleChoiceItems(options, position) { _, which ->
                        when {
                            options[which] == resources.getString(R.string.en_lang) -> {
                                LocaleHelper.setLocale(baseContext, "en").resources
                                alert.dismiss()
                                recreate()
                            }
                            options[which] == resources.getString(R.string.de_lang) -> {
                                LocaleHelper.setLocale(baseContext, "de").resources
                                alert.dismiss()
                                recreate()
                            }
                            else -> {
                                LocaleHelper.setLocale(baseContext, "ru").resources
                                alert.dismiss()
                                recreate()
                            }
                        }
                    }
                    .setNegativeButton(resources.getString(R.string.cancel)) { dialog, _ ->
                        dialog.cancel()
                    }
                alert = dialogBuilder.create()
                alert.show()

一切都适用于英语和德语。但是不适合俄罗斯人。 我还根据此post的第一个答案在LocaleHelper类中更改了Locale的输入,如下所示:

public static Context setLocale(Context context, String language) {
        persist(context, language);
        Configuration configuration;
        Resources resources;
        Locale locale = null;

        if (language.equals("ru")) {
            locale = new Locale(language, "RU");
            Locale.setDefault(locale);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration = context.getResources().getConfiguration();
            configuration.setLocale(locale);
            configuration.setLayoutDirection(locale);
            return context.createConfigurationContext(configuration);
        }

        locale = new Locale(language);
        Locale.setDefault(locale);
        resources = context.getResources();
        configuration = resources.getConfiguration();
        configuration.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            configuration.setLayoutDirection(locale);
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        return context;
    }

如何使它运行俄语。当用户选择它时,应用程序会将语言改为英语。

非常感谢!

1 个答案:

答案 0 :(得分:0)

我发现了问题!

我改变了

if (language.equals("ru")) {
        locale = new Locale(language, "RU");
        Locale.setDefault(locale);
}

if (language.equals("ru"))
        locale = new Locale(language, "RU");
else
        locale = new Locale(language);
Locale.setDefault(locale);

并删除

locale = new Locale(language);
Locale.setDefault(locale);

第二条if语句之后