Application.java
/**
* 设置国家语言
* @param language
*/
public static void selectLanguage(Context minContext , String language) {
Log.i("cs","selectLanguage="+language);
Locale targetLocale = null;
switch (language) {
case "en_us": //英语
targetLocale = Locale.ENGLISH;
break;
case "zh_cn": //简体中文
targetLocale = Locale.SIMPLIFIED_CHINESE;
break;
case "zh_tw": //繁体中文
targetLocale = Locale.TAIWAN;
break;
default: //默认繁体
targetLocale = Locale.TAIWAN;
//targetLocale = Locale.getDefault();
Log.i("cs","进入了默认语言"+language);
break;
}
Configuration configuration = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(targetLocale);
} else {
configuration.locale = targetLocale;
}
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
//保存设置语言的类型
SharedPreferencesUtils.putInfo(context,"language", language);
}
BaseActivity.java
@Override
protected void attachBaseContext(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String language = SharedPreferencesUtils.getPreferences(this).getString("language","");
Locale targetLocale = null;
switch (language) {
case "en_us": //英语
targetLocale = Locale.ENGLISH;
break;
case "zh_cn": //简体中文
targetLocale = Locale.SIMPLIFIED_CHINESE;
break;
case "zh_tw": //繁体中文
targetLocale = Locale.TAIWAN;
break;
default: //默认繁体
targetLocale = Locale.TAIWAN;
//targetLocale = Locale.getDefault();
Log.i("cs","进入了默认语言"+language);
break;
}
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale locale=targetLocale;
configuration.setLocale(locale);
context = context.createConfigurationContext(configuration);
} else {
// 这里是调用
MyApplication.selectLanguage(this,SharedPreferencesUtils.getPreferences(this).getString("language",""));
}
super.attachBaseContext(context);
}
别人写的帮助类
public class MultiLanguageUtil {
private static final String TAG = "MultiLanguageUtil";
private static MultiLanguageUtil instance;
private Context mContext;
public static final String SAVE_LANGUAGE = "save_language";
public static void init(Context mContext) {
if (instance == null) {
synchronized (MultiLanguageUtil.class) {
if (instance == null) {
instance = new MultiLanguageUtil(mContext);
}
}
}
}
public static MultiLanguageUtil getInstance() {
if (instance == null) {
throw new IllegalStateException("You must be init MultiLanguageUtil first");
}
return instance;
}
private MultiLanguageUtil(Context context) {
this.mContext = context;
}
/**
* 设置语言
*/
public void setConfiguration() {
Locale targetLocale = getLanguageLocale();
Configuration configuration = mContext.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(targetLocale);
} else {
configuration.locale = targetLocale;
}
Resources resources = mContext.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
}
/**
* 如果不是英文、简体中文、繁体中文,默认返回简体中文
*
* @return
*/
private Locale getLanguageLocale() {
int languageType = CommSharedUtil.getInstance(mContext).getInt(MultiLanguageUtil.SAVE_LANGUAGE, 0);
if (languageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
Locale sysLocale= getSysLocale();
return sysLocale;
} else if (languageType == LanguageType.LANGUAGE_EN) {
return Locale.ENGLISH;
} else if (languageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
return Locale.SIMPLIFIED_CHINESE;
} else if (languageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
return Locale.TRADITIONAL_CHINESE;
}
getSystemLanguage(getSysLocale());
Log.e(TAG, "getLanguageLocale" + languageType + languageType);
return Locale.SIMPLIFIED_CHINESE;
}
private String getSystemLanguage(Locale locale) {
return locale.getLanguage() + "_" + locale.getCountry();
}
//以上获取方式需要特殊处理一下
public Locale getSysLocale() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = LocaleList.getDefault().get(0);
} else {
locale = Locale.getDefault();
}
return locale;
}
/**
* 更新语言
*
* @param languageType
*/
public void updateLanguage(int languageType) {
CommSharedUtil.getInstance(mContext).putInt(MultiLanguageUtil.SAVE_LANGUAGE, languageType);
MultiLanguageUtil.getInstance().setConfiguration();
EventBus.getDefault().post(new OnChangeLanguageEvent(languageType));
}
public String getLanguageName(Context context) {
int languageType = CommSharedUtil.getInstance(context).getInt(MultiLanguageUtil.SAVE_LANGUAGE,LanguageType.LANGUAGE_FOLLOW_SYSTEM);
if (languageType == LanguageType.LANGUAGE_EN) {
return mContext.getString(R.string.setting_language_english);
} else if (languageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
return mContext.getString(R.string.setting_simplified_chinese);
} else if (languageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
return mContext.getString(R.string.setting_traditional_chinese);
}
return mContext.getString(R.string.setting_language_auto);
}
/**
* 获取到用户保存的语言类型
* @return
*/
public int getLanguageType() {
int languageType = CommSharedUtil.getInstance(mContext).getInt(MultiLanguageUtil.SAVE_LANGUAGE, LanguageType.LANGUAGE_FOLLOW_SYSTEM);
if (languageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
return LanguageType.LANGUAGE_CHINESE_SIMPLIFIED;
} else if (languageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
return LanguageType.LANGUAGE_CHINESE_TRADITIONAL;
} else if (languageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
return LanguageType.LANGUAGE_FOLLOW_SYSTEM;
}
Log.e(TAG, "getLanguageType" + languageType);
return languageType;
}
public static Context attachBaseContext(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return createConfigurationResources(context);
} else {
MultiLanguageUtil.getInstance().setConfiguration();
return context;
}
}
@TargetApi(Build.VERSION_CODES.N)
private static Context createConfigurationResources(Context context) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale locale=getInstance().getLanguageLocale();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
}
LINKS
记APP实现多语言(国际化)过程,兼容Android 8.0以上
Android国际化(多语言)实现,支持8.0+
MultiLanguages
AndroidMultiLanguage