学而实习之 不亦乐乎

Android:国际化(一)

2021-12-18 22:59:06

一、跟随系统,实现语言切换

这个最好实现,只需要定义好对应语言的 string.xml 文件就可以实现了。创建方法如下:

  1. 新建values包: 在该工程res中右击New —> Directory—> 输入对应国家的values值 (如:你想适配英文,包名就是”values-en-rUS”),再创建一个”values-zh-rCN”values包。
  2. 新建strings.xml放入相应的values位置 ,这时候会显示该国对应的国旗。
  3. 在对应国家的string.xml中编辑字符串

也可以通过对话框的形式进行创建,这里不再赘述。

创建好 string.xml 后,当设置 Android 系统的语言时,界面会跟随系统语言进行改变。但不自主进行设置。下面来看看怎么在代码中进行自主语言设置。

二、代码实现

为了实现自动切换功能,在上面设置好 string.xml 的基础上,封装一个LanguageUtil。

public class LanguageUtil {
    private static final String LOCALE_SP = "LOCALE_SP";
    private static final String LOCALE_SP_KEY = "LOCALE_SP_KEY";

    public static Locale getLocale(Context context) {
        SharedPreferences spLocale = context.getSharedPreferences(LOCALE_SP, Context.MODE_PRIVATE);
        String localeJson = spLocale.getString(LOCALE_SP_KEY, "");
        Gson gson = new Gson();
        return gson.fromJson(localeJson, Locale.class);
    }

    private static void setLocale(Context pContext, Locale pUserLocale) {
        SharedPreferences spLocal = pContext.getSharedPreferences(LOCALE_SP, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = spLocal.edit();
        String json = new Gson().toJson(pUserLocale);
        edit.putString(LOCALE_SP_KEY, json);
        edit.apply();
    }

    public static boolean updateLocale(Context context, Locale locale) {
        if (needUpdateLocale(context, locale)) {
            Configuration configuration = context.getResources().getConfiguration();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                configuration.setLocale(locale);
            } else {
                configuration.locale = locale;
            }
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            context.getResources().updateConfiguration(configuration, displayMetrics);
            setLocale(context, locale);
            return true;
        }
        return false;
    }

    public static boolean needUpdateLocale(Context pContext, Locale newUserLocale) {
        return newUserLocale != null && !getCurrentLocale(pContext).equals(newUserLocale);
    }
    public static Locale getCurrentLocale(Context context) {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言
            locale = context.getResources().getConfiguration().getLocales().get(0);
        } else {
            locale = context.getResources().getConfiguration().locale;
        }
        return locale;
    }
}

紧接着我们创建一个Application来自动加载

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        changeLang();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        changeLang();
    }

    private void changeLang() {
        Locale locale = LanguageUtil.getLocale(this);
        LanguageUtil.updateLocale(this, locale);
    }
}

在AndroidManifest.xml中配置一下:

 android:name=".MyApplication"