保存用户选择的语言,Android

时间:2015-09-27 23:35:34

标签: java android eclipse manifest lang

如何更改默认的string.xml?我可以将我的应用程序更改为五种不同的语言,但每当我关闭应用程序并重新打开它时。它可以追溯到默认的英语。

是否有可能以某种方式更改默认文件夹"值"。例如,从值(英语)默认为值-pt(葡萄牙语)。

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:7)

您可以使用以下

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

和onCreate()

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

答案 1 :(得分:2)

你可以试试这个:

改变:

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

保存:

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

更新:

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

加载:

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

在MainActivity中,您可以检查应用正在使用的默认语言:

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

然后,如果语言是您想要的语言,请使用check with statement,您应该执行updateText()函数:

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

不要忘记在MainActivity中执行LoadLanguage()以加载用户保存的语言。

答案 2 :(得分:1)

这是一些对我有用的代码:

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

}

来源:https://blog.lokalise.co/android-app-localization/

答案 3 :(得分:0)

您可以写信,然后阅读SharedPreferences以管理用户选择的语言

保存:

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

加载:

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

加载语言时,修改语言环境以使用它

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