Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

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

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?

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.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...