微信公众号:CodingAndroid
CSDN:
blog.csdn.net/xinpengfei5…
声明:本文由CodingAndroid原创,未经授权,不可随意转载!
最近,我们公司的业务已经拓展到了香港,我们都知道香港使用的是繁体中文,因此,我们的APP要可以设置繁体语言,这不我们要紧跟国际的步伐,实现多语言,产品定给我们的需求主要以实现简体中文、繁体中文、英文三种语言切换即可,具体的业务逻辑是:当用户第一次进入APP时,App的语言跟随当前系统语言,当用户设置了某种语言之后就切换为用户设置的语言,不管系统之后设置成哪种语言,都不会影响用户设置的语言,如果用户一直没有设置语言选项,只要系统语言改变时,APP的语言也要跟随系统语言设置改变。
说明:本文以实现简体中文、繁体中文和英语为例进行简要讲解。
1.实现思路
我们可以预先使用SharedPreference来保存一个语言类型的值,当用户第一次进入APP时,我们通过Key取出这个值,第一次肯定是取不到的,这时我们将App的语言设置为当前系统默认值即可;当用户通设置多语言时,我们将这个语言对应的值通过SharedPreference保存到本地即可,然后此时重启APP,设置为用户设置的语言即可,以后每次进入App时只需取出保存用户设置的语言设置即可,一般设置语言写在程序的入口
Application
的
onCreate()
方法里。
2.自定义SharedPreference工具类
作用:用于保存当前设置语言的类型,此文以SharedPreference保存为例,当然使用数据库或者其他保存方式也是可以的。
public class SpUtil {
private static final String APP_SP = "app_sp";
private static final String TAG = SpUtil.class.getSimpleName();
private SpUtil() {
}
private static SpUtil instance = new SpUtil();
private static SharedPreferences mSp = null;
public static SpUtil getInstance() {
if (mSp == null) {
mSp = MyApplication.getContext().getSharedPreferences(APP_SP, Context.MODE_PRIVATE);
}
return instance;
}
public void save(String key, Object value) {
if (value == null) {
Log.e(TAG, "value==null保存失败");
return;
}
if (value instanceof String) {
mSp.edit().putString(key, (String) value).commit();
} else if (value instanceof Boolean) {
mSp.edit().putBoolean(key, (Boolean) value).commit();
} else if (value instanceof Integer) {
mSp.edit().putInt(key, (Integer) value).commit();
}
}
public String getString(String key, String defValue) {
return mSp.getString(key, defValue);
}
public boolean getBoolean(String key, boolean defValue) {
return mSp.getBoolean(key, defValue);
}
public int getInt(String key, int defValue) {
return mSp.getInt(key, defValue);
}
public void clearAll() {
mSp.edit().clear().commit();
}
}
3.自定义LocaleUtil工具类
作用:用于设置保存语言及获取当前语言,重启APP等操作
public class LocaleUtil {
public static Locale getUserLocale() {
int currentLanguage = SpUtil.getInstance().getInt("currentLanguage", 0);
Locale myLocale = Locale.SIMPLIFIED_CHINESE;
switch (currentLanguage) {
case 0:
myLocale = Locale.SIMPLIFIED_CHINESE;
break;
case 1:
myLocale = Locale.ENGLISH;
break;
case 2:
myLocale = Locale.TRADITIONAL_CHINESE;
break;
}
return myLocale;
}
public static void changeAppLanguage(Context context) {
if (context == null) return;
Context appContext = context.getApplicationContext();
int currentLanguage = SpUtil.getInstance().getInt("currentLanguage", -1);
Locale myLocale;
switch (currentLanguage) {
case 0:
myLocale = Locale.SIMPLIFIED_CHINESE;
break
;
case 1:
myLocale = Locale.TRADITIONAL_CHINESE;
break;
case 2:
myLocale = Locale.ENGLISH;
break;
default:
myLocale = appContext.getResources().getConfiguration().locale;
}
if (needUpdateLocale(appContext, myLocale)) {
updateLocale(appContext, myLocale);
}
}
public static void changeAppLanguage(Context context, int currentLanguage) {
if (context == null) return;
Context appContext = context.getApplicationContext();
SpUtil.getInstance().save("currentLanguage", currentLanguage);
Locale myLocale = Locale.SIMPLIFIED_CHINESE;
switch (currentLanguage) {
case 0:
myLocale = Locale.SIMPLIFIED_CHINESE;
break;
case 1:
myLocale = Locale.TRADITIONAL_CHINESE;
break;
case 2:
myLocale = Locale.ENGLISH;
break;
}
if (LocaleUtil.needUpdateLocale(appContext, myLocale)) {
LocaleUtil.updateLocale(appContext, myLocale);
}
Toast.makeText(appContext, appContext.getString(R.string.set_success), Toast.LENGTH_SHORT).show();
restartApp(appContext);
}
public static void restartApp(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public static Locale getCurrentLocale(Context context) {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = context.getResources().getConfiguration().getLocales().get(0);
} else {
locale = context.getResources().getConfiguration().locale;
}
return locale;
}
public static void updateLocale(Context context, Locale locale) {
if (needUpdateLocale(context, locale)) {
Configuration configuration = context.getResources().getConfiguration();
if
(Build.VERSION.SDK_INT >= 19) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
context.getResources().updateConfiguration(configuration, displayMetrics);
}
}
public static boolean needUpdateLocale(Context context, Locale locale) {
return locale != null && !getCurrentLocale(context).equals(locale);
}
public static void setLanguage(Context context, Configuration newConfig) {
if (context == null) return;
Context appContext = context.getApplicationContext();
int currentLanguage = SpUtil.getInstance().getInt("currentLanguage", -1);
Locale locale;
switch (currentLanguage) {
case 0:
locale = Locale.SIMPLIFIED_CHINESE;
break;
case 1:
locale = Locale.TRADITIONAL_CHINESE;
break;
case 2:
locale = Locale.ENGLISH;
break;
default:
locale = appContext.getResources().getConfiguration().locale;
}
if (locale != null) {
Locale.setDefault(locale);
Configuration configuration = new Configuration(newConfig);
if (Build.VERSION.SDK_INT >= 19) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
appContext.getResources().updateConfiguration(configuration, appContext.getResources().getDisplayMetrics());
}
}
}
3.1Application中调用,用于初始化语言设置
@Override
public void onCreate() {
super.onCreate();
LocaleUtil.changeAppLanguage(this);
}
3.2在Application中重写如下方法:用于当系统设置语言变化时进行语言设置
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e("TAG", "onConfigurationChanged");
LocaleUtil.setLanguage(mContext, newConfig);
}
3.3在多语言设置界面设置APP语言
当用户点击保存时传入当前语言对应的Index值即可,我这里使用的是保存一个Int类型的值,每一个Int类型的值对应一种语言的类型,当然你可以根据自己的实现保存为String类型也是可以的。
LocaleUtil.changeAppLanguage(mContext, currentLanguage