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