最近在做国际化功能,这里讲过程中的技术点记录一下。
第一。如果切换不同语言之后读取不同的values语言内容
1.所有的activity中(一般都是baseactivit中),重写下面的方法
- /**
- * 设置修改语言
- *
- * @param newBase
- */
- @Override
- protected void attachBaseContext(Context newBase) {
- Context context = MyContextWrapper.wrap(newBase);
- super.attachBaseContext(context);
- }
/**
* 设置修改语言
*
* @param newBase
*/
@Override
protected void attachBaseContext(Context newBase) {
Context context = MyContextWrapper.wrap(newBase);
super.attachBaseContext(context);
}
2.重写contextwarpper类
- public class MyContextWrapper extends android.content.ContextWrapper {
- public MyContextWrapper(Context base) {
- super(base);
- }
- public static ContextWrapper wrap(Context context) {
- Locale newLocale;
- switch (context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).getInt(MainActivity.LANGUAGE, -1)) {
- case -1://没有做过设置,读取手机语言
- String locale = Locale.getDefault().getLanguage();
- if (TextUtils.isEmpty(locale)) {
- context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();
- newLocale = Locale.CHINESE;
- MainActivity.sLanguage = 1;
- } else if (locale.contains(“en”)) {
- context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 2).commit();
- newLocale = Locale.ENGLISH;
- MainActivity.sLanguage = 2;
- } else {
- context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();
- newLocale = Locale.CHINESE;
- MainActivity.sLanguage = 1;
- }
- break;
- case 2://设置为英语
- newLocale = Locale.ENGLISH;
- MainActivity.sLanguage = 2;
- break;
- default://默认为汉语
- newLocale = Locale.CHINESE;
- MainActivity.sLanguage = 1;
- break;
- }
- Resources res = context.getResources();
- Configuration configuration = res.getConfiguration();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- configuration.setLocale(newLocale);
- LocaleList localeList = new LocaleList(newLocale);
- LocaleList.setDefault(localeList);
- configuration.setLocales(localeList);
- context = context.createConfigurationContext(configuration);
- } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- configuration.setLocale(newLocale);
- context = context.createConfigurationContext(configuration);
- }
- return new ContextWrapper(context);
- }
- }
public class MyContextWrapper extends android.content.ContextWrapper {
public MyContextWrapper(Context base) {
super(base);
}
public static ContextWrapper wrap(Context context) {
Locale newLocale;
switch (context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).getInt(MainActivity.LANGUAGE, -1)) {
case -1://没有做过设置,读取手机语言
String locale = Locale.getDefault().getLanguage();
if (TextUtils.isEmpty(locale)) {
context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();
newLocale = Locale.CHINESE;
MainActivity.sLanguage = 1;
} else if (locale.contains("en")) {
context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 2).commit();
newLocale = Locale.ENGLISH;
MainActivity.sLanguage = 2;
} else {
context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();
newLocale = Locale.CHINESE;
MainActivity.sLanguage = 1;
}
break;
case 2://设置为英语
newLocale = Locale.ENGLISH;
MainActivity.sLanguage = 2;
break;
default://默认为汉语
newLocale = Locale.CHINESE;
MainActivity.sLanguage = 1;
break;
}
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
}
return new ContextWrapper(context);
}
}
第二.经过上面的步骤已经可以实现切换语言之后读取不同的values下面的内容值了。但是呢,由于现在都是app内部实现语言切换,所以如果仅仅实现上面的步骤,那么仅仅实现了系统语言切换app也跟着切换语言。下面就是实现app内部实现语言切换的过程。
1.app内部切换语言通常都是会进入一个单独的页面,然后切换语言。当切换语言页面关闭的时候呢,我们需要将任务栈中的所有页面都清空。这个过程如下:
- Intent intent = new Intent(this, MainActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
- overridePendingTransition(R.anim.change_language_enter, R.anim.change_language_exit);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.anim.change_language_enter, R.anim.change_language_exit);
这样就可以实现了。上面加入了页面关闭的切换动画,为了是切换更加顺畅。但是会有如下问题。
2.上面的操作如果mainactivity有大量的任务需要做,比如嵌套很多的fragment等等,那么就会出现明显的闪屏现象,非常不友好,为此需要给mainactivity的主题增加如下属性:
- <item name=“android:windowDisablePreview”>true</item>
<item name="android:windowDisablePreview">true</item>
至此,一个优雅的切换国际化语言的方案结束了。
demo下载地址 :https://download.youkuaiyun.com/download/zhq217217/10448158
</div>