简体   繁体   English

在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. 我希望根据用户的喜好设置应用程序语言,但到目前为止,我的工作方式并不理想。

I have set the default values: strings.xml and also values-es with a strings.xml inside in spanish. 我已经设置了默认值:strings.xml以及使用西班牙语中的strings.xml的values-es。 I have a menu option which brings the user to a Preference activity where he can amon gother things chose the language. 我有一个菜单选项,它将用户带到一个Preference活动,在那里他可以选择语言。

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());
            }
        }
    }

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. 因此,当我更改它的语言时,但是当我稍后回来或重新启动模拟器时,语言将返回到默认语言环境en_US,并且应用程序语言将再次更改为默认语言。 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 ? 我知道我可以获得这个偏好(我可以从我的所有活动中访问)然后每次设置语言环境但我发现它有点重,是不是有办法以更优雅的方式做到这一点

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. 我想要做的是,如果用户设置语言,那么当他2天后回来时,他不必再次更改语言。

Any ideas? 有任何想法吗?

OK it may help someone. 好吧,它可以帮助某人。 I have added the folowing to the main activity manifest: 我已将以下内容添加到主要活动清单中:

android:configChanges="locale"

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. 然后,当用户选择首选项时,我放置了一个确认按钮,然后此按钮将您带到主要活动,这就是为什么lnagages被重置。

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);
    }
}

end at every activity I have like 20 of them I call this method before: 在每个活动结束时,我喜欢其中的20个,我之前称之为:

setContentView(R.layout.list_event);

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 使用这些方法,当我旋转屏幕时,活动不会改变语言,这是指向帮助我的博客的链接: http//adrianvintu.com/blogengine/post/Force-Locale-on-Android.aspx

I would think that you need to be setting the locale in the MainActivity onCreate method. 我认为您需要在MainActivity onCreate方法中设置区域设置。 The same way you are setting it when the onSharedPreferenceChanged method. onSharedPreferenceChanged方法设置时onSharedPreferenceChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM