更新有关语言更改的ListPreference摘要 [英] Update ListPreference summary on language change
问题描述
当在我的ListPreference
中选择了一种新语言时,我将其更改并重新加载我的Fragment
以便应用它,例如:
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;
}
});
每个示例,如果选择了法语,它将确实将法语应用于我的所有字符串(在strings-fr.xml
文件中定义),但是对于我的ListPreference
摘要却不这样做(即使它们具有它们的在正确的文件中翻译).
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).
两个问题:
-
是否有更好的方法来加载我的语言,而不是重新加载我的
Fragment
?
如何将ListPreference
摘要更新为正确的语言?
And how may I update my ListPreference
s summary to the right language?
推荐答案
有没有更好的方法来加载我的语言,而不是重新加载我的语言 碎片?
Is there a better way to load my language instead of reloading my Fragment?
我认为这是立即加载新语言的最佳方法.但是,Activity
/Application
重新启动后,它可能不会继续.在这种情况下,您需要检查SharedPreferences
中的语言并将其加载到Activity
/Application
onCreate()
:
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. In that case, you would need to check the language from SharedPreferences
and load it in the 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);
...
}
如何将ListPreferences摘要更新为正确的语言?
And how may I update my ListPreferences summary to the right language?
您的问题是缺少XML文件,但这是我通常的做法,每次都可以正常工作,而无需以编程方式更新摘要:
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:
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
<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
<string-array name="language_names">
<item>Anglais</item>
<item>Français</item>
</string-array>
<string name="current_language">Français</string>
这篇关于更新有关语言更改的ListPreference摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!