项目上有个需求,app要实现多语言,在app内部设置语言,可以选择是否跟随系统或指定语言;
主要思路:
1、app本地保存所设置的语言;
2、每个页面及App类的生命周期中判断当时语言是否是所设置语言,如果不是,刚更新configuration信息;
fun checkLanguage(context: Context): Context {
val language = CommonApp.prefs?.LANGUAGE
val config = context.resources.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
when(language) {
LANG_ZH -> config.setLocale(Locale.CHINESE)
LANG_EN -> config.setLocale(Locale.ENGLISH)
else -> config.setLocale(context.resources.configuration.locales[0])
}
return context.createConfigurationContext(config)
} else {
when(language) {
LANG_ZH -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(Locale.CHINESE)
} else {
config.locale = Locale.CHINESE
}
}
LANG_EN -> {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(Locale.ENGLISH)
} else {
config.locale = Locale.ENGLISH
}}
else -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(Locale.getDefault())
} else {
config.locale = Locale.getDefault()
}
}
}
context.resources.updateConfiguration(config, context.resources.displayMetrics)
return context
}
APP和BaseActivity中均要做如下实现:
override fun attachBaseContext(base: Context) {
super.attachBaseContext(LanguageUtil.checkLanguage(base))
}
参考其它App如微信等语言支持的实现,在语言设置页面更改了语言设置之后,为了让语言变更立刻生效,建议重新打跳转至主页:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
整个流程大概是这样,进行了多种测量没出现异常,网上一些多语言切换教程中遇到的一些异常,大概两种:
1、强杀app后,语言又恢复到系统默认了;(App每次启动都要读取存在本地的语言信息,更新到所设置语言即可)
2、app内更换语言后,在手机系统里切换语言,结果回到app,又变成系统语言了,强杀重启app又好了;(最初的实现版本我也遇到这种问题了,第一版的解决方案是,如果系统语言切换了,就重启app进入MainActivity,貌似微信也是这么干的;但后面改为上面的代码后又没有这个问题了,考虑到是系统版本的兼容性问题)
上面的checkLanguage方法里面有几个版本的兼容的处理:
1、重新设置Config中的语言时,在不同的版本中会涉及到不同的变量
/**
* Current user preference for the locale, corresponding to
* <a href="{@docRoot}guide/topics/resources/providing-resources.html#LocaleQualifier">locale</a>
* resource qualifier.
*
* @deprecated Do not set or read this directly. Use {@link #getLocales()} and
* {@link #setLocales(LocaleList)}. If only the primary locale is needed,
* <code>getLocales().get(0)</code> is now the preferred accessor.
*/
@Deprecated public Locale locale;(api 17以前直接设置locale)
private LocaleList mLocaleList; (api 24之后语言信息存在LocaleList里面,当前语言即mLocaleList(0))
2、更新Configuration的两种方法:
/** * Store the newly updated configuration. * * @deprecated See {@link android.content.Context#createConfigurationContext(Configuration)}. */ @Deprecated public void updateConfiguration(Configuration config, DisplayMetrics metrics) { updateConfiguration(config, metrics, null); } api 24以前用此方法更新config
/** * Return a new Context object for the current Context but whose resources * are adjusted to match the given Configuration. Each call to this method * returns a new instance of a Context object; Context objects are not * shared, however common state (ClassLoader, other Resources for the * same configuration) may be so the Context itself can be fairly lightweight. * * @param overrideConfiguration A {@link Configuration} specifying what * values to modify in the base Configuration of the original Context's * resources. If the base configuration changes (such as due to an * orientation change), the resources of this context will also change except * for those that have been explicitly overridden with a value here. * * @return A {@link Context} with the given configuration override. */ public abstract Context createConfigurationContext( @NonNull Configuration overrideConfiguration); api 24之后改为此方法了,返回context对象,需要重新app到对应的activity或application代码中完成上面的兼容性处理后,app不需要强杀重启便可正常的切换语言,在4.4.4和7.1的机子上测量都没出现问题;