简体   繁体   中英

Localize all my application programmatically

I want to locate all my application programmatically. For this, I have created the required folders:

所需的字符串文件夹

If I change the language of my device, the language is changed in my application. But I want choose the language.

For that purpose, I have created a Preference activity. In the end of this activity, I have written this code:

...    
mPreferenceLeguage.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object value) {

                leng = value.toString();
                int lengIndex = Integer.parseInt(leng) - 1;

                // Update leguage summary
                mPreferenceLeguage.setDefaultValue(lengIndex);
                mPreferenceLeguage.setSummary(mPreferenceLeguage.getEntries()[lengIndex]);

                if(leng.equals("1")){
                    saveLocale("en");
                } else if(leng.equals("2")){
                   saveLocale("eu");
                } else{
                    saveLocale("es");
                }
                return true;
            }
        });


    }


    public void saveLocale(String lang) {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(langPref, lang);
        editor.commit();
    }

And in MainActivity, the following code:

...

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        loadLocale();

...


    public void loadLocale() {
        String langPref = "Language";
        SharedPreferences prefs = getSharedPreferences("CommonPrefs",
                Activity.MODE_PRIVATE);
        String language = prefs.getString(langPref, "");
        changeLang(language);
    }

    public void changeLang(String lang) {
        if (lang.equalsIgnoreCase(""))
            return;
        myLocale = new Locale(lang);
        Locale.setDefault(myLocale);
        android.content.res.Configuration config = new android.content.res.Configuration();
        config.locale = myLocale;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
    }

The language is changed, but not immediately. The language is changes when I restart the application.

I want this change will be immediate. For it, I have tried to recall MainActivity from Preference activity, at the end of saveLocale() method.

What can I do to solve it?

/**Use below code Spinet it will work fine you need to recreate your
activity to change the language instantly without re-starting the app**/

Note: language ---> "en" or "eu" or "es". Based on your preference

private void updateLanguage(String language)
{
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

    SharedPreferences languagepref = getSharedPreferences("language",MODE_PRIVATE);
    SharedPreferences.Editor editor = languagepref.edit();
    editor.putString("languageToLoad",language);
    editor.apply();

    recreate();
}

Override
public void recreate()
{
    if (android.os.Build.VERSION.SDK_INT >= 14)
    {
        super.recreate();
    }
    else
    {
        startActivity(getIntent());
        finish();
    }
}

To pickup configuration changes, such as locale changes, please make sure you have registered for the appropriate configuration change on your Activity in the Android manifest:

https://developer.android.com/reference/android/R.attr.html#configChanges

You can handle the configuration change in your Activity (after having registered properly) by catching the change in onConfigurationChanged .

See Activity#onConfigurationChanged docs for more information: https://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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