When a new language is selected in my ListPreference
, I change it and reload my Fragment
in order to apply it, like such:
listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Get selected language
String selectedLanguage = newValue.toString();
preference.setSummary(selectedLanguage);
// Change language
Locale locale;
if (selectedLanguage.equals("French")) {
locale = new Locale("fr");
} else {
locale = new Locale("en");
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getActivity().getApplicationContext().getResources().updateConfiguration(config, null);
// Refresh fragment
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, new SettingsFragment())
.commit();
return true;
}
});
Per exemple, if French is chosen, it does apply the French language to all of my strings (defines in a strings-fr.xml
file) but it does not do so for my ListPreference
s summaries (even though they have their translation in the right files).
Two questions:
Is there a better way to load my language instead of reloading my
Fragment
?And how may I update my
ListPreference
s summary to the right language?