1

I changed my android app language by this method:

private void setLocal(String language) {

    Locale myLocale = new Locale(language);
    Resources res = getActivity().getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    HeaderSettingFragment frag = new HeaderSettingFragment();
    getFragmentManager().beginTransaction()
            .replace(R.id.setting_container, frag).addToBackStack(null).commit();
}

and language changed correctly, but when I close the app and open it again the previous language remained.

How to avoid that?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Behrooz Fard
  • 536
  • 4
  • 26

2 Answers2

1

Try this .

LanguageUtils

public class LanguageUtils {

/**
 * change language
 *
 * @param context
 * @param language
 */
public static void shiftLanguage(Activity context, String language) {
    if (language.equals("en")) {
        Locale.setDefault(Locale.ENGLISH);
        Configuration config = context.getResources().getConfiguration();
        config.locale = Locale.ENGLISH;
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        SharedPreferenceUtils.createSP(context, "en");
    } else {
        Locale.setDefault(Locale.CHINESE);
        Configuration config = context.getResources().getConfiguration();
        config.locale = Locale.CHINESE;
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        SharedPreferenceUtils.createSP(context, "zh");
    }
    context.recreate();
}

/**
 * app start setting language
 *
 * @param context
 * @param language
 */
public static void startLanguage(Context context, String language) {
    if (language.equals("zh")) {
        Locale.setDefault(Locale.CHINESE);
        Configuration config = context.getResources().getConfiguration();
        config.locale = Locale.CHINESE;
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    } else {
        Locale.setDefault(Locale.ENGLISH);
        Configuration config = context.getResources().getConfiguration();
        config.locale = Locale.ENGLISH;
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    }
}
}

Use it

switch (item.getItemId()) {
      case R.id.hn:
           String languageToLoad = "hi";
           LanguageUtils.shiftLanguage(this, languageToLoad);
           break;
      case R.id.eng:
           languageToLoad = "en"; // your language
           LanguageUtils.shiftLanguage(this, languageToLoad);
           break;

      default:
}

And use it in Application

When you restart app , it will set Language.

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    if (TextUtils.equals(SharedPreferenceUtils.getLanguage(this), "en")) {
        LanguageUtils.startLanguage(getApplicationContext(), "en");
    } else {
        LanguageUtils.startLanguage(getApplicationContext(), "zh");
    }
}

}

SharedPreferenceUtils

public class SharedPreferenceUtils {

private static final String SP_NAME = "sp";
public static final String LANGUAGE = "language";


// create
public static boolean createSP(Context context, String language) {
    SharedPreferences.Editor editor = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE).edit();
    editor.putString(LANGUAGE, language);
    return editor.commit();
}


// get language
public static String getLanguage(Context context) {
    SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    return sp.getString(LANGUAGE, "");
}

}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

You have to save the language state somehow. For example in a SharedPreference so you can read it out when the app starts. Actually you are just chaning, but noch saving the state.

Pynnie
  • 136
  • 12
  • If I saved language in sharedPreference each time I open the app I should call setLocal() but the main activity language was not changed until go to other activity and return to it. – Behrooz Fard Oct 18 '17 at 14:53