赞
踩
@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); } }
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(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。