如何在所有活动中更改我的应用程序的语言?

时间:2018-05-06 12:11:03

标签: android sharedpreferences locale

我已经在第一个Activity中成功更改了我的应用程序的语言..但是通过移动到另一个Activity,我发现该语言没有改变。

请注意,我使用以下代码更改语言

form

我使用了一个对话框来选择语言并将其放在共享首选项中,如下所示

public static final String languageFile="language_file";
SharedPreferences myLang;

private Locale Language;
String defaultLanguage="en";
String language1;

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

    myLang = getSharedPreferences(languageFile, Context.MODE_PRIVATE);
    language1=myLang.getString("language",defaultLanguage).toString();

    setLocale(language1);
}

public void setLocale(String lang) {

    Language = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = Language;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this,Welcome_Activity.class);
    startActivity(refresh);
    finish();
}

2 个答案:

答案 0 :(得分:0)

以下为我效劳:

public void setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
    invalidateOptionsMenu();
    recreate();
}

答案 1 :(得分:0)

您只需创建一个BaseClass并使用基类扩展您的所有活动

public class MainActivity  extends BaseActivity  {

   @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

}


/**
 * @author Atif Abbasi .
 */

public class BaseActivity  extends AppCompatActivity {


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }




    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LocaleHelper.wrap(newBase, AppClass.getLanguage(newBase)));

/**
*save value in sharedPrefrences 
*
*AppClass.getLanguage(newBase)
*
*
**/

    }


    /**
     * A locale helper class for updating app specific locale
     */
    private static class LocaleHelper extends ContextWrapper {

        public LocaleHelper(Context base) {
            super(base);
        }

        public static ContextWrapper wrap(Context context, String language) {
            if (TextUtils.isEmpty(language.trim())) {
                return new LocaleHelper(context);
            }
            Configuration config = context.getResources().getConfiguration();
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                config.setLocale(locale);
            } else {
                //noinspection deprecation
                config.locale = locale;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                config.setLayoutDirection(locale);
                context = context.createConfigurationContext(config);
            } else {
                //noinspection deprecation
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
            return new LocaleHelper(context);
        }

    } // LocaleHelper


}