/**
* 更改应用语言
*/
public class LangugeUtils {
/**
* 更改应用语言
*
* @param context
* @param languageNameShort
*/
public static void changeAppLanguage(Context context, String languageNameShort) {
DisplayMetrics metrics =context. getResources().getDisplayMetrics();
Configuration configuration =context.getResources().getConfiguration();
switch (languageNameShort){
case "zh_CN":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(Locale.SIMPLIFIED_CHINESE);
} else {
configuration.locale = Locale.SIMPLIFIED_CHINESE;
}
break;
case "zh_TW":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(Locale.TRADITIONAL_CHINESE);
} else {
configuration.locale = Locale.TRADITIONAL_CHINESE;
}
break;
case "en":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(Locale.ENGLISH);
} else {
configuration.locale = Locale.ENGLISH;
}
break;
}
context.getResources().updateConfiguration(configuration, metrics);
//重新启动Activity
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
}
}
//第二种
//application中
@Override
public void onCreate() {
super.onCreate();
appContext = getApplicationContext();
intance = this;
context = this;
registerActivityLifecycleCallbacks(callbacks);
}
ActivityLifecycleCallbacks callbacks = new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
String language = (String) SPUtils.get(getApplicationContext(), SPUtils.language, "");
String country = (String) SPUtils.get(getApplicationContext(), SPUtils.country, "");
if (!TextUtils.isEmpty(language) && !TextUtils.isEmpty(country)) {
//强制修改应用语言
if (!MultiLanguageUtils.isSameWithSetting(activity)) {
Locale locale = new Locale(language, country);
MultiLanguageUtils.changeAppLanguage(activity, locale, false);
// activity.recreate();
}
}
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
//Activity 其它生命周期的回调
};
@Override
protected void attachBaseContext(Context base) {
//系统语言等设置发生改变时会调用此方法,需要要重置app语言
super.attachBaseContext(MultiLanguageUtils.attachBaseContext(base));
}
//工具类
public class MultiLanguageUtils {
public static Context attachBaseContext(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return createConfigurationResources(context);
} else {
setConfiguration(context);
return context;
}
}
/**
* 设置语言
*
* @param context
*/
public static void setConfiguration(Context context) {
Locale appLocale = getAppLocale(context);
//如果本地有语言信息,以本地为主,如果本地没有使用默认Locale
Locale locale = null;
String language = (String) SPUtils.get(context, SPUtils.language, "");
String country = (String) SPUtils.get(context, SPUtils.country, "");
if (!TextUtils.isEmpty(language) && !TextUtils.isEmpty(country)) {
if (isSameLocal(appLocale, language, country)) {
locale = appLocale;
} else {
locale = new Locale(language, country);
}
} else {
locale = appLocale;
}
Configuration configuration = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
}
@TargetApi(Build.VERSION_CODES.N)
private static Context createConfigurationResources(Context context) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale appLocale = getAppLocale(context);
//如果本地有语言信息,以本地为主,如果本地没有使用默认Locale
Locale locale = null;
String language = (String) SPUtils.get(context, SPUtils.language, "");
String country = (String) SPUtils.get(context, SPUtils.country, "");
if (!TextUtils.isEmpty(language) && !TextUtils.isEmpty(country)) {
if (isSameLocal(appLocale, language, country)) {
locale = appLocale;
} else {
locale = new Locale(language, country);
}
} else {
locale = appLocale;
}
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
return context.createConfigurationContext(configuration);
}
/**
* 更改应用语言
*
* @param
* @param locale 语言地区
* @param persistence 是否持久化
*/
public static void changeAppLanguage(Context context, Locale locale, boolean persistence) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
Configuration configuration = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
resources.updateConfiguration(configuration, metrics);
if (persistence) {
saveLanguageSetting(context, locale);
}
}
//保存多语言信息到sp中
public static void saveLanguageSetting(Context context, Locale locale) {
SPUtils.set(context, SPUtils.language, locale.getLanguage());
SPUtils.set(context, SPUtils.country, locale.getCountry());
}
//获取本地应用的实际的多语言信息
public static Locale getAppLocale(Context context) {
//获取应用语言
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale locale = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = configuration.getLocales().get(0);
} else {
locale = configuration.locale;
}
return locale;
}
//判断sp中和app中的多语言信息是否相同
public static boolean isSameWithSetting(Context context) {
Locale locale = getAppLocale(context);
String language = locale.getLanguage();
String country = locale.getCountry();
String sp_language = (String) SPUtils.get(context, SPUtils.language, "");
String sp_country = (String) SPUtils.get(context, SPUtils.country, "");
if (language.equals(sp_language) && country.equals(sp_country)) {
return true;
} else {
return false;
}
}
public static boolean isSameLocal(Locale appLocale, String sp_language, String sp_country) {
String appLanguage = appLocale.getLanguage();
String appCountry = appLocale.getCountry();
if (appLanguage.equals(sp_language) && appCountry.equals(sp_country)) {
return true;
} else {
return false;
}
}
//修改应用内语言设置
public static void changeLanguage(Context context, String language, String area) {
if (TextUtils.isEmpty(language) && TextUtils.isEmpty(area)) {
LogUtils.log("changeLanguage", language+area);
//如果语言和地区都是空,那么跟随系统
SPUtils.set(context, SPUtils.language, language);
SPUtils.set(context, SPUtils.country, area);
} else {
//不为空,那么修改app语言,并true是把语言信息保存到sp中,false是不保存到sp中
Locale newLocale = new Locale(language, area);
changeAppLanguage(context, newLocale, true);
}
//重启app,这一步一定要加上,如果不重启app,可能打开新的页面显示的语言会不正确
Intent intent = new Intent(BaseApplication.appContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
BaseApplication.appContext.startActivity(intent);
// android.os.Process.killProcess(android.os.Process.myPid());
}
}