更改应用语言

时间:2017-10-18 14:47:00

标签: java android

我通过这种方法改变了我的Android应用程序语言:

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

并且语言已正确更改,但当我关闭应用并再次打开时,之前的语言仍然存在。

如何避免?

2 个答案:

答案 0 :(得分:1)

试试这个。

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

使用

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:
}

并在应用程序中使用

重新启动应用时,它会设置语言。

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

}

答案 1 :(得分:0)

你必须以某种方式保存语言状态。例如,在SharedPreference中,您可以在应用启动时将其读出。实际上你只是在追逐,但没有拯救国家。