1
votes

I'm including localization to translate my app intro three different languages , after setting up the translated strings of each laguages , i go to preference settings and i check a checkbox and and the translation works fine , the problem is that when i restart the app , localization even though i have saved the chosen language in sharedpreference and retreiving it in mainactivity

*This is how i m setting the languages

var sharedPreferences = requireContext().getSharedPreferences("prefs", 
           Context.MODE_PRIVATE)
 var editor = sharedPreferences.edit()

 spanishCheckBox.setOnPreferenceChangeListener(object : Preference.OnPreferenceChangeListener{
                override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
                    var isSpanishChecked = newValue as Boolean
                    if(isSpanishChecked){
                        var Lang = "es"
                        editor.putString("key",Lang)
                        editor.apply()
                        var local = Locale(Lang)
                        var configuration = Configuration()
                        configuration.locale = local
                        resources.updateConfiguration(configuration,resources.displayMetrics)

                        englishCheckBox.isChecked = false
                        frenchCheckBox.isChecked = false

                        Intent(requireContext(),MainActivity::class.java).also {
                            it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                            startActivity(it)
                        }
                    }
                    return true
                }
            })
  • This is how i m retreiving data in mainactivity

 fun LoadLanguageConfiguration(){
        var sharedpreferences = getSharedPreferences("prefs", Context.MODE_PRIVATE)
        var langCode = sharedpreferences.getString("key","")
        var local = Locale(langCode)
        var configuration = Configuration()
        configuration.locale = local
        baseContext.createConfigurationContext(configuration)
    }

The translation only works when i set the language in the app , but when i restart the app , it goes back to default language which is english , any help would be appreciated , thank you

1
Did you mean that sharedpreference is deleting the value of "key" when the app restarting? or the value still same as before restarting but the language not changing.Nanda R.M
upon checking using the debugger , the value is retreived normally but it is not being applied to localization , even when i set the value manually , the app language is not changedTaki
Where do you call LoadLanguageConfiguration(), you should call it very early stage of your app startup.Sinan Ceylan
I'm calling in in the oncreate , so as soon as the app lauches , it should check that methodTaki
Would you try to call LoadLanguageConfiguration() at onStart function ?. As the android lifecycle onStart called after onRestart and after oncreate.Nanda R.M

1 Answers

0
votes

The problem is that i was not starting my language loading method at the start and before everything , so i put it as first method in my oncreate and everything works as supposed to , hope this helps someone else