文章参考
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
resetLanguageSetting(mLanguage);
}
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";
public static final String ZH_CN = "S";
public static final String ZH_HK = "C";
public static final String EN_US = "E";
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);
}
public static String getDefaultLanguage(Context context) {
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
Locale contextLocale = config.locale;
return contextLocale.toString();
}
}
|