在 Android 偏好设置中设置应用程序语言 [英] Set-up the application Language in Android Preferences
问题描述
我希望根据用户偏好设置应用程序语言,但到目前为止它还不能按照我的意愿工作.
I would like the application language to be set according to the user preferences but up till now it doesn't work how I would like it to.
我已经设置了默认值:strings.xml 和 values-es,其中包含西班牙语的 strings.xml.我有一个菜单选项,可将用户带到偏好活动,他可以在其中选择语言.
I have set the default values: strings.xml and also values-es with a strings.xml inside in spanish. I have a menu option which brings the user to a Preference activity where he can amon gother things chose the language.
所以这里是一些代码的摘录:
So here are some extracts of the code:
public class Preference extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
......
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
...}
//(......)
//and here I have the listener so when the language pref changes value the locale gets changed.
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("listPref2")) {
String idioma = sharedPreferences.getString("listPref2", "catala");
if ("castella".equals(idioma)) {
idioma = "es_ES";
Locale locale = new Locale(idioma);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
}
}
因此,当我更改语言时,它可以工作,但是当我稍后回来或重新启动模拟器时,语言会恢复为默认语言环境 en_US,并且应用程序语言会再次更改回默认值.我能做些什么来解决这个问题?
So when I change the language it works but then when I come back later or restart the emulator the language gets back to default locale the en_US and the app language gets changed back to default again. What can I do to sort that out?
我知道我可以得到这个偏好(我可以从我的所有活动中访问它),然后每次设置语言环境,但我发现它有点重,没有办法在更 优雅的方式?
I know I can get this preference (which I can access to from all my activities) and then each time set up the locale but I find it a bit heavy isn't there a way to do it in a more elegant way?
我想做的是,如果用户设置了语言,那么当他 2 天后回来时,他不必再次更改语言.
What I would like to do is if the user sets up the language so when he comes back 2 days later he doesn't have to change the language again.
有什么想法吗?
推荐答案
好的,它可能对某人有所帮助.我已将以下内容添加到主要活动清单中:
OK it may help someone. I have added the folowing to the main activity manifest:
android:configChanges="locale"
然后,当用户选择首选项时,我放置了一个确认按钮,然后此按钮将您带到主要活动,这就是重置 lnagages 的原因.
Then when the user choses the preferences I have put a confirm button and then this button brings you to main activity that is why the lnagages gets reset.
我有一个静态类,我有这个代码来更改语言环境:
I have a static class where I have this code to change the locale:
public static void updateLanguage(Context context, String idioma) {
if (!"".equals(idioma)) {
if ("castella".equals(idioma)) {
idioma = "es_ES";
} else if ("catala".equals(idioma)) {
idioma = "ca_ES";
}
Locale locale = new Locale(idioma);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, null);
}
}
在我喜欢的 20 个活动中结束我之前调用此方法:
end at every activity I have like 20 of them I call this method before:
setContentView(R.layout.list_event);
使用这些方法,当我旋转屏幕时,活动不会改变语言这是一个帮助我的博客的链接:http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx
With these methods when I rotate the screen the activities don't change the language here is a link to a blog that helped me: http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx
这篇关于在 Android 偏好设置中设置应用程序语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!