学而实习之 不亦乐乎

Android:国际化(二)

2021-12-18 22:59:18

一、简介

应用内设置多语言,可随系统语言改变而改变,也可设置app为固定语言不受系统语言影响

二、实现原理

  1. Application的onCreate中初始化,根据本地 SharePreference 中保存的语言信息来选择显示哪种语言
  2. 在设置界面选择对应语言,然后把语言信息保存到 SharePreference 中。

三、实现代码

1、首先要有一个多语言方法类 InternationalUtil

public class InternationalUtil {

    public static Context attachBaseContext(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return createConfigurationResources(context);
        } else {
            setConfiguration(context);
            return context;
        }
    }

    /**
     * 设置语言
     *
     * @param context
     */
    public static void setConfiguration(Context context) {
        Locale appLocale = getAppLocale(context);

        //如果本地有语言信息,以本地为主,如果本地没有使用默认Locale
        Locale locale = null;
        String spLanguage = SpUtil.getString(context, ConstantGlobal.LOCALE_LANGUAGE);
        String spCountry = SpUtil.getString(context, ConstantGlobal.LOCALE_COUNTRY);
        if (!TextUtils.isEmpty(spLanguage) && !TextUtils.isEmpty(spCountry)) {
            if (isSameLocal(appLocale, spLanguage, spCountry)) {
                locale = appLocale;
            } else {
                locale = new Locale(spLanguage, spCountry);
            }
        } else {
            locale = appLocale;
        }

        Configuration configuration = context.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context createConfigurationResources(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale appLocale = getAppLocale(context);

        //如果本地有语言信息,以本地为主,如果本地没有使用默认Locale
        Locale locale = null;
        String spLanguage = SpUtil.getString(context, ConstantGlobal.LOCALE_LANGUAGE);
        String spCountry = SpUtil.getString(context, ConstantGlobal.LOCALE_COUNTRY);
        if (!TextUtils.isEmpty(spLanguage) && !TextUtils.isEmpty(spCountry)) {
            if (isSameLocal(appLocale, spLanguage, spCountry)) {
                locale = appLocale;
            } else {
                locale = new Locale(spLanguage, spCountry);
            }
        } else {
            locale = appLocale;
        }

        configuration.setLocale(locale);
        configuration.setLocales(new LocaleList(locale));
        return context.createConfigurationContext(configuration);
    }

    /**
     * 更改应用语言
     *
     * @param
     * @param locale      语言地区
     * @param persistence 是否持久化
     */
    public static void changeAppLanguage(Context context, Locale locale, boolean persistence) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(locale);
            configuration.setLocales(new LocaleList(locale));
            context.createConfigurationContext(configuration);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        resources.updateConfiguration(configuration, metrics);

        if (persistence) {
            saveLanguageSetting(context, locale);
        }
    }

    //保存多语言信息到sp中
    public static void saveLanguageSetting(Context context, Locale locale) {
        SpUtil.saveString(context, ConstantGlobal.LOCALE_LANGUAGE, locale.getLanguage());
        SpUtil.saveString(context, ConstantGlobal.LOCALE_COUNTRY, locale.getCountry());
    }

    //获取本地应用的实际的多语言信息
    public static Locale getAppLocale(Context context) {
        //获取应用语言
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = configuration.getLocales().get(0);
        } else {
            locale = configuration.locale;
        }
        return locale;
    }

    //判断sp中和app中的多语言信息是否相同
    public static boolean isSameWithSetting(Context context) {
        Locale locale = getAppLocale(context);
        String language = locale.getLanguage();
        String country = locale.getCountry();

        String sp_language = SpUtil.getString(ConstantGlobal.LOCALE_LANGUAGE);
        String sp_country = SpUtil.getString(ConstantGlobal.LOCALE_COUNTRY);
        if (language.equals(sp_language) && country.equals(sp_country)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isSameLocal(Locale appLocale, String sp_language, String sp_country) {
        String appLanguage = appLocale.getLanguage();
        String appCountry = appLocale.getCountry();
        if (appLanguage.equals(sp_language) && appCountry.equals(sp_country)) {
            return true;
        } else {
            return false;
        }
    }
}

2、Application 中初始化

根据 SharePreference 保存的语言信息做初始化。如下:

  1. attachBaseContext() 中的初始化,应用刚启动的时候此方法在 onCreate() 前执行,当用户修改手机系统的默认语言时,也会调用 attachBaseContext() 方法
  2. onCreate() 中一定要加上 registerActivityLifecycleCallbacks(callbacks);用来监听 Activity,如果不是 SharePreference 中设定的语言的话,可以修改成对应 SharePreference 中的语言。

注意:InternationalApp 需要在 AndroidManifest.xml 中注册。如下:

<application
    android:name=".InternationalApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.InternationalDemo">
public class InternationalApp extends Application {

    private static Context mAppContext;

    public static Context getContext() {
        return mAppContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        this.mAppContext = getApplicationContext();
        //初始化本地语言,这句话可以不要,因为在attachBaseContext中已经处理了
        //changeLanguage();
        //注册Activity生命周期监听回调,此部分一定加上,因为有些版本不加的话多语言切换不回来
        registerActivityLifecycleCallbacks(callbacks);
    }

    ActivityLifecycleCallbacks callbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            String language = SpUtil.getString(getApplicationContext(), ConstantGlobal.LOCALE_LANGUAGE);
            String country = SpUtil.getString(getApplicationContext(), ConstantGlobal.LOCALE_COUNTRY);
            if (!TextUtils.isEmpty(language) && !TextUtils.isEmpty(country)) {
                //强制修改应用语言
                if (!InternationalUtil.isSameWithSetting(activity)) {
                    Locale locale = new Locale(language, country);
                    InternationalUtil.changeAppLanguage(activity, locale, false);
                }
            }
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
        //Activity 其它生命周期的回调
    };

    @Override
    protected void attachBaseContext(Context base) {
        //系统语言等设置发生改变时会调用此方法,需要要重置app语言
        super.attachBaseContext(InternationalUtil.attachBaseContext(base));
    }
}

3、语言切换

注意:在设置语言后应用重启了,如果不重启,会出现有界面语言没有切换的问题。

例如:当设置简体中文时,在响应的点击事件中调用 changeLanguage("zh","CN") 。如下:

//修改应用内语言设置
private void changeLanguage(String language, String area) {
    if (TextUtils.isEmpty(language) && TextUtils.isEmpty(area)) {
        //如果语言和地区都是空,那么跟随系统
        SpUtil.saveString(ConstantGlobal.LOCALE_LANGUAGE, "");
        SpUtil.saveString(ConstantGlobal.LOCALE_COUNTRY, "");
    } else {
        //不为空,那么修改app语言,并true是把语言信息保存到sp中,false是不保存到sp中
        Locale newLocale = new Locale(language, area);
        InternationalUtil.changeAppLanguage(MainActivity.this, newLocale, true);
        InternationalUtil.changeAppLanguage(InternationalApp.getContext(),
                newLocale, true);
    }
    //重启app,这一步一定要加上,如果不重启app,可能打开新的页面显示的语言会不正确
    Intent intent = new Intent(InternationalApp.getContext(), MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    InternationalApp.getContext().startActivity(intent);
    android.os.Process.killProcess(android.os.Process.myPid());
}