user3513645
user3513645

Reputation: 3

Error with multilanguage in android?

I'm making multi language for my android project. Everything is perfect. Except when I close the application and start it again, it turns back to the previous language. For Example: I have 2 languages Vietnamese and English. When I start it's Vietnamese. Then I choose English. But when I close and start it again it turns back to Vietnamese?

My code:

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

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

        }

    });
    return settingView;
}

Upvotes: 0

Views: 235

Answers (2)

Alejandro Cumpa
Alejandro Cumpa

Reputation: 2363

You are not saving that value, so when you close the app all values are setted again, and the app choose the language from the device. try using the sharedPreferences to achieve that:

SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);

To read preferences:

 //use this when you start your activty 
 String lan = prefs.getString("language", Locale.getDefault().getLanguage() );
setlocale(lan);

To edit and save preferences

  //so when the users pick from the list you change the "en" with other language
prefs.edit().putString("language", "en").apply();

Upvotes: 1

Floern
Floern

Reputation: 33904

When you override the Locale it's only as long active as the current process is running. After closing the application the process gets killed and your Locale setting is lost.

If you want to save the custom Locale you may have to store it permanently, e.g. in the SharedPreferences as described in this answer: https://stackoverflow.com/a/18563447/559745 Then every time you start the app you need to check which Locale was saved.

Upvotes: 1

Related Questions