建立在Android的preferences应用程序语言 [英] Set-up the application Language in Android Preferences
问题描述
我想申请语言根据用户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中,也重视-ES具有的strings.xml里面西班牙语。我有一个菜单选项,它使用户在preference活动,在那里他可以阿蒙gother东西选择的语言。
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.
因此,这里的code一些摘录:
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?
我知道我能得到这个preference(我可以从我的所有活动获得),然后每次都设置了语言环境,但我觉得有点重是不是有办法做到这一点的更多的优雅的方式?
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?
如果用户设置了这样的语言时,他回来两天后,他没有再改变语言我想要做的就是。
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:
安卓configChanges =区域设置
然后,当用户的动产的preferences我已经把一个确认按钮,然后此按钮,您的主要活动,这就是为什么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.
我有一个静态类,我有这个code要更改语言环境:
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);
使用这些方法时,我旋转屏幕的活动不更改语言 这里是一个链接到一个博客,帮助我: <一href="http://adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx">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的preferences应用程序语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!