Maulik
Maulik

Reputation: 43

LocaleManager for change language not work in Marshmallow device

I'm trying to change the language in my App using LocaleManager. My code runs perfectly on Nougat and Oreo, but on Marshmallow it's not working. I have a Settings Activity, where I add one dialog box to change the Language. Here is my code:

LocaleManager.kt :

class LocaleManager {

    companion object {

        val LANGUAGE_ENGLISH = "en"
        private val LANGUAGE_KEY = "display_lang"

        fun setNewLocale(c: Context, language: String): Context {
            persistLanguage(c, language)
            return updateResources(c, language)
        }

        private fun persistLanguage(c: Context, language: String) {
            val prefs = PreferenceManager.getDefaultSharedPreferences(c)
            prefs.edit().putString(LANGUAGE_KEY, language).apply()
        }

        private fun updateResources(context: Context, language: String): Context {
            val locale = Locale(language)
            Locale.setDefault(locale)
            val config = Configuration(context.resources.configuration)
            config.setLocale(locale)
            return context.createConfigurationContext(config)
        }

        fun setLocale(c: Context): Context {
            return updateResources(c, getLanguage(c))
        }

        fun getLanguage(c: Context): String {
            val prefs = PreferenceManager.getDefaultSharedPreferences(c)
            return prefs.getString(LANGUAGE_KEY, LANGUAGE_ENGLISH)
        }
    }
}

This is my Setting Activity.kt for Custom Dialog:

fun showChangeLangDialog() {
    val factory = LayoutInflater.from(this)
    val langDialog = factory.inflate(R.layout.select_lang_dialog, null)
    val langList = resources.getStringArray(R.array.languages)
    langList.forEach {
        val obj = JSONObject(it)
        val rdbtnLang = RadioButton(this)
        rdbtnLang.id = obj.getInt("id")
        rdbtnLang.text = obj.getString("lang")
        Log.e("Lan", LocaleManager.getLanguage(this))
        if (LocaleManager.getLanguage(this) == obj.getString("code")) {
            rdbtnLang.isChecked = true
        }
        rdbtnLang.textSize = 20f
        rdbtnLang.setPadding(20, 30, 30, 30)
        langDialog.selectLangList.addView(rdbtnLang)
    }

    AlertDialog.Builder(this)
            .setTitle(R.string.selectLang)
            .setPositiveButton(R.string.ok) { dialog, i ->
                langList.forEach {
                    val obj = JSONObject(it)
                    if (langDialog.selectLangList.checkedRadioButtonId == obj.getInt("id")) {
                        setNewLocale(obj.getString("code"))
                    }
                }
                dialog.dismiss()
            }
            .setNegativeButton(R.string.cancel) { dialog, whichButton ->
                dialog.dismiss()
            }
            .setView(langDialog)
            .create()
            .show()
}

In every activity where I need to change the language text, I add this snippet:

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

Upvotes: 4

Views: 1116

Answers (1)

Ravi Patel
Ravi Patel

Reputation: 309

i think you have to change your string type "gu-rIN" to simple "gu" type translation

Upvotes: 1

Related Questions