I'm including localization to translate my app intro three different languages, after setting up the translated strings of each language, 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
}
})
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.
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.