简体   繁体   中英

I can't change language on my android app by system setting

I have two strings.xml files, one is default, the other is values-zh . The problem is whatever I need to change the phone system language , my app strings always is Chinese. But if I use such fuction code to change language , it works.

 public static final String EN = "en";
 public static final String ZH = "zh";

  public void changeLanguage(String language) {
    Resources resources = mContext.getResources();
    Configuration configuration = resources.getConfiguration();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    if(language.equals(ZH)){
        configuration.locale = Locale.CHINESE;
    }else if(language.equals(EN)){
        configuration.locale = Locale.ENGLISH;
    }else {
        configuration.locale = Locale.ENGLISH;
        language = EN;
    }
    resources.updateConfiguration(configuration , displayMetrics);
    AbSharedUtil.putString(mContext , "language" , language);
    mContext.finish();
    Intent intent = new Intent(mContext , MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    mContext.startActivity(intent);



}

So the problem is I can change language by code , but I can't not change it by system language settings automatically.

try to use below code.

        Locale myLocale = new Locale("zh");
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);

确认清单中未添加带有参数“ locale”的android:configChanges ,这将导致您的应用在系统区域设置更改时不会重新加载资源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM