-->

Save language chosen by user, Android

2019-02-20 01:20发布

问题:

How can I change default string.xml? I can change my app to five different languages, but whenever I close the app and reopen it. It goes back to default English.

Is it possible to somehow change the default folder "values". For example, from values (English) which is default to values-pt (Portuguese).

Any help is much appreciated!

回答1:

you can use following

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);
        saveLocale(lang);
        Locale.setDefault(myLocale);
        android.content.res.Configuration config = new android.content.res.Configuration();
        config.locale = myLocale;
        getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());

    }

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 onCreate()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_language);
        loadLocale();
    }


回答2:

You can try this:

To Change:

public void changeLanguage(String language) {
    locale = new Locale(language);
    saveLocale(language);
    Locale.setDefault(locale);
    conf.locale = locale;
    getBaseContext().getResources().updateConfiguration(conf, null);
    updateText();
}

To Save:

    public void saveLocale(String language) {
    SharedPreferences sharedPreferences = getSharedPreferences("com.example.myapp.PREFERENCES", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("USER_LANGUAGE", language);
    editor.commit();
}

To Update:

    private void updateText() {
    btn_chat.setText(R.string.live_chat);
}

To Load:

public void LoadLanguage(){
    SharedPreferences shp = getSharedPreferences(
            "com.example.myapp.PREFERENCES",Context.MODE_PRIVATE);
    String language = shp.getString("USER_LANGUAGE","");
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}

In MainActivity you can check the default language that app is using:

language = getResources().getString(R.string.lang);

Then if the language is the language you want by check with statement, you should perform updateText() function:

if (language.equals("zh")) {
        btn_lang.setText(R.string.someName);
        updateText();
    }

Do not forget to perform the LoadLanguage() in MainActivity to Load the language that user saved.



回答3:

Here is some code that works for me:

public class  MainActivity extends AppCompatActivity {
public static String storeLang;

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this);
    storeLang = shp.getString(getString(R.string.key_lang), "");

    // Create a new Locale object
    Locale locale = new Locale(storeLang);

    // Create a new configuration object
    Configuration config = new Configuration();
    // Set the locale of the new configuration
    config.locale = locale;
    // Update the configuration of the Accplication context
    getResources().updateConfiguration(
            config,
            getResources().getDisplayMetrics()
    );

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

Source: https://blog.lokalise.co/android-app-localization/



回答4:

You can write to, then read from SharedPreferences to manage the user's chosen language

To save:

SharedPreferences shp = getSharedPreferences(
        "com.example.yourapp.PREFERENCES",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putString("USER_LANGUAGE", language);
editor.commit();

To load:

SharedPreferences shp = getSharedPreferences(
        "com.example.yourapp.PREFERENCES",Context.MODE_PRIVATE);
String language = shp.getString("USER_LANGUAGE","en");

When you load the language, modify the locale to use it

Locale locale = new Locale(language);  
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getResources().updateConfiguration(config, getResources().getDisplayMetrics());