如何在多语言中保存当前语言?

时间:2015-04-18 06:34:05

标签: android android-fragments android-fragmentactivity multilingual

我正在制作一个多语言的android项目。 我做到了,但是当我关闭应用程序时,它又回到了默认语言。 我在google搜索并搜索了stackoverflow上的一些主题。他们说,我必须使用SharedPreferences保存当前语言。我试过了,但它没有用。 PLz检查我做错了什么。 我的代码:

public class setting extends Fragment {

/**
 * @param args
 */
private Spinner spinnerctrl;
private Locale myLocale;
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View settingView =  inflater.inflate(R.layout.setting, container, false);
    spinnerctrl = (Spinner) settingView.findViewById(R.id.spinner1);
    spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 == 1) {
                Toast.makeText(arg0.getContext(), "You have selected English", Toast.LENGTH_SHORT).show();
                setLocale("en");
            } else if (arg2 == 2) {
                Toast.makeText(arg0.getContext(), "You have selected VietNam", Toast.LENGTH_SHORT).show();
                setLocale("vi");
            }
        }

        private void setLocale(String lang) {
            // TODO Auto-generated method stub
            SharedPreferences prefs = getActivity().getSharedPreferences(
                      "com.example.app", Context.MODE_PRIVATE);
            myLocale = new Locale(lang);
            Resources res = getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.locale = myLocale;
            res.updateConfiguration(conf, dm);
            Intent refresh = new Intent(getActivity(), MainActivity.class);
            startActivity(refresh);
            String lan = prefs.getString("language", Locale.getDefault().getLanguage() );
            setLocale(lan);
            prefs.edit().putString("language", "en").apply();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }

    });
    return settingView;
}

1 个答案:

答案 0 :(得分:0)

setLocale()中,首先应存储首选项,然后检索它。你是以相反的方式做的。所以,

prefs.edit().putString("language", "en").apply();

应该先来,然后

String lan = prefs.getString("language", Locale.getDefault().getLanguage() );

更新功能:

private void setLocale(String lang) {
    /*SharedPreferences prefs = getActivity().getSharedPreferences(
              "com.example.app", Context.MODE_PRIVATE);*/

    SharedPreferences setPrefs = getApplicationContext().getSharedPreferences("com.example.app", MODE_PRIVATE);
    SharedPreferences.Editor editor = setPrefs.edit();
    editor.putString("language", "en");
    editor.commit();

    myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(getActivity(), MainActivity.class);
    startActivity(refresh);

    /*prefs.edit().putString("language", "en").apply();
    String lan = prefs.getString("language", Locale.getDefault().getLanguage() );
    //setLocale(lan);   //Remove this. It will create infinite loop */

    SharedPreferences getPrefs = getApplicationContext().getSharedPreferences("com.example.app", MODE_PRIVATE);
    String lan = getPrefs.getString("language", "");
    System.out.println("-- locale from getPrefs " + lan);

}

要详细了解SharedPreferences,请查看此link