androidlocalizationlocale

How to set Russian Language over Locale in android? Android App Localization


My app supports 3 languages (English, German, Russian). I use for language settings a LocaleHelper class and set language as follows:

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()

Everything works fine for English and German. But doesn't for Russian one. I also changed the input for Locale in the LocaleHelper class according to the first answer of this post as follows:

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

How can I get it run for Russian language? When the user selects it, the app changes the language to English instead.


Solution

  • I found the problem!

    I changed the

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

    to

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

    and removed

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

    after the second if statement