applySystemLanguage : 设置系统语言
applyLanguage : 设置语言
isAppliedLanguage : 是否设置了语言
getAppliedLanguage : 获取设置的语言
getContextLanguage : 获取上下文的语言
getAppContextLanguage : 获取应用上下文的语言
getSystemLanguage : 获取系统的语言
updateAppContextLanguage: 更新应用上下文语言
attachBaseContext : 如果设置语言无效则在 Activity#attachBaseContext 调用它

原理

如果我们的应用不设置 android:configChanges="locale|layoutDirection",那么应用是跟随系统语言设置的变化而变化的,比如你应用适配了英语(values-en-rUS)和简体中文(values-zh-rCN),那么你去设置里切换成英语的话,返回到你应用中,你的 Activity 会重新创建一遍,把 Activity#Resource#Configuration#Locale 设置为当前系统语言,这样就达到了跟随系统语言设置的变化而变化,但 Application 并没有重启,所以这就导致了一开说到的问题。

要解决跟随系统变化这一点的话,只需要在 Activity#onCreate 的生命周期中把 Application#Resource#Configuration#Locale 设置为系统的 Locale 即可,那么系统的 Locale 怎么读取呢,知道之前屏幕适配方案的人应该也能想到这一方式:Resources.getSystem().getConfiguration().locale,这样,我们的 Application 便也切换成了系统语言,注意更新 Locale 需要兼容下高版本,调用具体代码可以参照如下:

/**
• Update the locale of applicationContext.
• 
• @param destLocale The dest locale.
• @param consumer The consumer.
 */
 public static void updateAppContextLanguage(@NonNull Locale destLocale, @Nullable Utils.Consumer consumer) {
 pollCheckAppContextLocal(destLocale, 0, consumer);
 }
 static void pollCheckAppContextLocal(final Locale destLocale, final int index, final Utils.Consumer consumer) {
 Resources appResources = Utils.getApp().getResources();
 Configuration appConfig = appResources.getConfiguration();
 Locale appLocal = getLocal(appConfig);
 setLocal(appConfig, destLocale);
 // 由于 updateConfigure 并不会立马更新(本地测试小于 1ms 便会更新)config,所以需要后面的轮询来监听变化来做其他处理(比如重启应用)
 Utils.getApp().getResources().updateConfiguration(appConfig, appResources.getDisplayMetrics());if (consumer == null) return;
if (isSameLocale(appLocal, destLocale)) {
 consumer.accept(true);
 } else {
 if (index < 20) {
 UtilsBridge.runOnUiThreadDelayed(new Runnable() {
 @Override
 public void run() {
 pollCheckAppContextLocal(destLocale, index + 1, consumer);
 }
 }, 16);
 return;
 }
 Log.e(“LanguageUtils”, “appLocal didn’t update.”);
 consumer.accept(false);
 }
 }

那么如果是应用内切换语言呢?我们可以仿照系统切换语言的方式,重启我们的应用或者重启所有的 Activity,在打开的 Activity#onCreate 中把 ActivityApplicationLocale 都设置为我们设置的语言即可,当然,这份设置是需要保存下来的,根据你的需求来确定是要保存在服务端还是本地。相关代码如下所示:

private static void applyLanguageReal(final Locale locale,
 final boolean isRelaunchApp) {
 if (locale == null) {
 // 后面的 true 使用的是 sp.commit,因为如果用 apply 的话,杀掉应用重启会来不及保存
 UtilsBridge.getSpUtils4Utils().put(KEY_LOCALE, VALUE_FOLLOW_SYSTEM, true);
 } else {
 UtilsBridge.getSpUtils4Utils().put(KEY_LOCALE, locale2String(locale), true);
 }
 Locale destLocal = locale == null ? getLocal(Resources.getSystem().getConfiguration()) : locale;
 // 这个就是上面提到的函数
 updateAppContextLanguage(destLocal, new Utils.Consumer() {
 @Override
 public void accept(Boolean success) {
 if (success) {
 restart(isRelaunchApp);
 } else {
 // use relaunch app
 UtilsBridge.relaunchApp();
 }
 }
 });
 }private static void restart(final boolean isRelaunchApp) {
 if (isRelaunchApp) {
 UtilsBridge.relaunchApp();
 } else {