当前位置:   article > 正文

Webview导致多语言切换混乱_webview 到时语言切换失败

webview 到时语言切换失败

文章参考

	@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        resetLanguageSetting(mLanguage);
    }

    /**
     * 返回Activity时,要重新设置language,防止语言错乱
     * WebView会引起语言错乱
     *
     * @param language
     */
    protected void resetLanguageSetting(String language) {
        if (TextUtils.equals(language, ZH_CN)) {
            //简体
            LanguageUtils.switchLanguage(getActivity(), Locale.SIMPLIFIED_CHINESE);
        } else if (TextUtils.equals(language, EN_US)) {
            //英文
            LanguageUtils.switchLanguage(getActivity(), Locale.US);
        } else {
            //繁体
            LanguageUtils.switchLanguage(getActivity(), Locale.TRADITIONAL_CHINESE);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
public class LanguageUtils {

    public static final String SWITCH_LANGUAGE = "SWITCH_LANGUAGE";

    /**
     * ZH_CN  简体
     * ZH_HK  繁体
     * EN_US  英文
     */
    public static final String ZH_CN = "S";
    public static final String ZH_HK = "C";
    public static final String EN_US = "E";


    /**
     * 切换语言环境
     *
     * @param context
     * @param locale
     */
    public static void switchLanguage(Context context, Locale locale) {
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            conf.setLocale(locale);
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            conf.setLocales(localeList);
        } else {
            conf.setLocale(locale);
        }
        res.updateConfiguration(conf, dm);
    }

    /**
     * 获取当前语言环境
     *
     * @param context
     * @return
     */
    public static String getDefaultLanguage(Context context) {
        Resources resources = context.getResources();
        Configuration config = resources.getConfiguration();
        Locale contextLocale = config.locale;
        return contextLocale.toString();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/196653
推荐阅读
相关标签
  

闽ICP备14008679号