前言,最近要实现多语言切换需求,在网上查了很多资料,基本实现了想要的效果。
主要代码:
Configuration configuration = activity.getResources().getConfiguration();
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
activity.getBaseContext().getResources().updateConfiguration(configuration,
activity.getBaseContext().getResources().getDisplayMetrics());
多语言切换前提是多语言资源,首先add需要的values文件夹,比如,本案例实现的是简体中文、繁体中文和英语环境,我就在res文件夹下另外add两个文件夹,values-en(英语资源文件夹)和values-zh-rTW(繁体中文资源文件夹),分别添加string文件,分别如图:
主要实现思路是通过sharePreference存储的标识,判断之前是否设置过语言环境,然后选择语言显示。
首先新建了一个工具类LocalUtils.class ,
/**
* Created by GerryRun on 2018/6/13.
* com.android.gerryrun.myanimdemo.utils
* email gerryin@163.com
*/
public class LocalUtils {
public static final String LOCAL_DEFAULT_LANGUAGE = "LOCAL_DEFAULT_LANGUAGE";
public static final String LOCAL_SIMPLE_CHINESE_LANGUAGE = "CN";
public static final String LOCAL_TRADITIONAL_CHINESE_LANGUAGE = "TW";
public static final String LOCAL_ENGLISH_LANGUAGE = "EN";
private static final String TAG = LocalUtils.class.getSimpleName();
private static boolean isCommit = false;
/***
* 设置语言环境
* @param activity
* @param locale 比如,Locale.ENGLISH
*/
public static void setLanguage(Activity activity, Locale locale) {
if (activity == null) {
throw new NullPointerException("activity cannot be null");
}
Configuration configuration = activity.getResources().getConfiguration();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
} else {
configuration.locale = locale;
}
activity.getBaseContext().getResources().updateConfiguration(configuration,
activity.getBaseContext().getResources().getDisplayMetrics());
}
/****
* 获取 SharedPreferences 保存的语言环境标识
* @return 保存的语言环境标识
*/
public static String getLanguage() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(GRApplication.getInstance());
return preferences.getString(LOCAL_DEFAULT_LANGUAGE, "TW");
}
/***
* 保存语言标识
* @param language
* @return
*/
public static boolean saveLanguage(String language) {
/***
* 通过sharepreference保存标识,
* 中文-LOCAL_SIMPLE_CHINESE_LANGUAGE,
* 英文-LOCAL_ENGLISH_LANGUAGE
* 繁体-LOCAL_TRADITIONAL_CHINESE_LANGUAGE
*/
if (language.equals(LOCAL_SIMPLE_CHINESE_LANGUAGE)) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(GRApplication.getInstance());
SharedPreferences.Editor editor = sp.edit();
editor.remove(LocalUtils.LOCAL_DEFAULT_LANGUAGE);
editor.putString(LocalUtils.LOCAL_DEFAULT_LANGUAGE, LOCAL_SIMPLE_CHINESE_LANGUAGE);
isCommit = editor.commit();
} else if (language.equals(LocalUtils.LOCAL_ENGLISH_LANGUAGE)) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(GRApplication.getInstance());
SharedPreferences.Editor editor = sp.edit();
editor.remove(LocalUtils.LOCAL_DEFAULT_LANGUAGE);
editor.putString(LocalUtils.LOCAL_DEFAULT_LANGUAGE, LOCAL_ENGLISH_LANGUAGE);
isCommit = editor.commit();
} else if (language.equals(LOCAL_TRADITIONAL_CHINESE_LANGUAGE)) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(GRApplication.getInstance());
SharedPreferences.Editor editor = sp.edit();
editor.remove(LocalUtils.LOCAL_DEFAULT_LANGUAGE);
editor.putString(LocalUtils.LOCAL_DEFAULT_LANGUAGE, LOCAL_TRADITIONAL_CHINESE_LANGUAGE);
isCommit = editor.commit();
}
Log.e(TAG, language);
return isCommit;
}
布局内容:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.android.gerryrun.myanimdemo.changeLanguage.ChangeLanguageActivity">
<Button android:id="@+id/bt_change_language" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/app_language_test" />
</android.support.constraint.ConstraintLayout>
</layout>
布局中的button的点击事件:
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_change_language:
if (LocalUtils.getLanguage().equals(LocalUtils.LOCAL_ENGLISH_LANGUAGE)) {
LocalUtils.setLanguage(mActivity, Locale.SIMPLIFIED_CHINESE);
isCommit = LocalUtils.saveLanguage(LocalUtils.LOCAL_SIMPLE_CHINESE_LANGUAGE);
} else if (LocalUtils.getLanguage().equals(LocalUtils.LOCAL_SIMPLE_CHINESE_LANGUAGE)) {
LocalUtils.setLanguage(mActivity, Locale.TRADITIONAL_CHINESE);
isCommit = LocalUtils.saveLanguage(LocalUtils.LOCAL_TRADITIONAL_CHINESE_LANGUAGE);
} else if (LocalUtils.getLanguage().equals(LocalUtils.LOCAL_TRADITIONAL_CHINESE_LANGUAGE)) {
LocalUtils.setLanguage(mActivity, Locale.ENGLISH);
isCommit = LocalUtils.saveLanguage(LocalUtils.LOCAL_ENGLISH_LANGUAGE);
}
//如果Editor.commit()成功
if (isCommit) {
Intent intent = new Intent(this, FlashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
// 杀掉进程,重启app
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
break;
}
}
效果是点击button重启APP,完成多语言显示替换,效果图如下: