Save language chosen by user, Android

荒凉一梦 提交于 2019-12-01 08:39:41

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

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.

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/

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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!