[英]Update ListPreference summary on language change
When a new language is selected in my ListPreference
, I change it and reload my Fragment
in order to apply it, like such: 当在
ListPreference
选择一种新语言时,我将其更改并重新加载Fragment
以便应用它,例如:
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). 举例来说,如果选择了法语,它将确实对所有我的字符串应用法语(在
strings-fr.xml
文件中定义),但对于ListPreference
的摘要却不这样做(即使它们在正确的文件)。
Two questions: 两个问题:
Is there a better way to load my language instead of reloading my Fragment
? 有没有更好的方法来加载我的语言而不是重新加载我的
Fragment
?
And how may I update my ListPreference
s summary to the right language? 以及如何将
ListPreference
的摘要更新为正确的语言?
Is there a better way to load my language instead of reloading my Fragment?
有没有更好的方法来加载我的语言而不是重新加载我的片段?
I think this would be the best way for an immediate load of the new langauage. 我认为这是立即加载新语言的最佳方法。 It may not continue after
Activity
/ Application
restart though. 但是,在“
Activity
/ Application
重新启动后,它可能不会继续。 In that case, you would need to check the language from SharedPreferences
and load it in the Activity
/ Application
onCreate()
: 在这种情况下,您需要从
SharedPreferences
检查语言并将其加载到Activity
/ Application
onCreate()
:
private Locale locale = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final Configuration config = getBaseContext().getResources().getConfiguration();
final String language = PreferenceManager.getDefaultSharedPreferences(this).getString("language", "");
if (!TextUtils.isEmpty(language) && !config.locale.getLanguage().equals(language))
{
locale = new Locale(language);
setLocale(config);
}
setContentView(R.layout.main_activity);
...
}
And how may I update my ListPreferences summary to the right language?
以及如何将我的ListPreferences摘要更新为正确的语言?
Your question is missing XML files but here is how I usually do that and it works just fine for me every time and I don't need to update the summary programmatically: 您的问题是缺少XML文件,但是这是我通常的工作方式,它每次都能正常工作,并且不需要以编程方式更新摘要:
preferences.xml preferences.xml
<ListPreference
android:defaultValue="en"
android:entries="@array/language_names"
android:entryValues="@array/language_values"
android:icon="@drawable/ic_language"
android:key="language"
android:summary="@string/current_language"
android:title="@string/language_title"/>
strings.xml strings.xml
<string-array name="language_names">
<item>English</item>
<item>French</item>
</string-array>
<string-array name="language_values">
<item>en</item>
<item>fr</item>
</string-array>
<string name="current_language">English</string>
strings-fr.xml strings-fr.xml
<string-array name="language_names">
<item>Anglais</item>
<item>Français</item>
</string-array>
<string name="current_language">Français</string>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.