Skip to content

App内语言设置

App使用的语言可以不跟随系统设定。

方法类

    // App使用的语言类别
    private static final String K_APP_LOCALE_LANGUAGE = "key_app_locale_language";

    public static Locale appLocale = Locale.ENGLISH; // app当前使用的locale

    // App支持的语言类型
    public static class SupportLanguage {
        public static final String EN = "en";
        public static final String ZH_CN = "zh-CN";
    }

    // App支持的语言
    public enum SupportLocale {
        EN(SupportLanguage.EN), ZH_CN(SupportLanguage.ZH_CN);
        String localeDesc;

        SupportLocale(String localeDesc) {
            this.localeDesc = localeDesc;
        }
    }

    // App启动,检查并存下当前手机语言类型
    public static void initAppLocale(Context context) {
        String lang = context.getSharedPreferences(CONTROL_SP_NAME, Context.MODE_PRIVATE).getString(K_APP_LOCALE_LANGUAGE, "");
//        Log.d(TAG, "设置的lang: " + lang);
        if (TextUtils.isEmpty(lang)) {
            String phoneLang = getPhoneLocale().getLanguage();
            if (Locale.ENGLISH.getLanguage().equals(phoneLang)) {
                saveCurLocaleName(context, SupportLanguage.EN);
            } else if (Locale.SIMPLIFIED_CHINESE.getLanguage().equals(phoneLang)) {
                saveCurLocaleName(context, SupportLanguage.ZH_CN);
            } else {
                saveCurLocaleName(context, SupportLanguage.EN); // 不在支持列表内则默认存英文
            }
        }
        lang = context.getSharedPreferences(CONTROL_SP_NAME, Context.MODE_PRIVATE).getString(K_APP_LOCALE_LANGUAGE, "");
        changeAppLocale(lang);
    }

    private static void changeAppLocale(String lang) {
        switch (lang) {
            case SupportLanguage.ZH_CN:
                appLocale = Locale.SIMPLIFIED_CHINESE;
                break;
            case SupportLanguage.EN:
            default:
                appLocale = Locale.ENGLISH;
                break;
        }
    }

    // 存储语言设置
    private static void saveCurLocaleName(Context context, String localeName) {
        context.getSharedPreferences(CONTROL_SP_NAME, Context.MODE_PRIVATE).edit()
                .putString(K_APP_LOCALE_LANGUAGE, localeName)
                .apply();
    }

    // 更改app当前语言
    public static void changeToLocaleName(Context context, String localeName) {
        saveCurLocaleName(context, localeName);
        changeAppLocale(localeName);
    }

    // 获取手机的语言设置
    private static Locale getPhoneLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }

    // Application更新语言设置
    public static void updateConfig(Application app, Configuration configuration) {
        if (appLocale != null) {
            //Wrapping the configuration to avoid Activity endless loop
            Configuration config = new Configuration(configuration);
            // We must use the now-deprecated config.locale and res.updateConfiguration here,
            // because the replacements aren't available till API level 24 and 17 respectively.
            config.locale = appLocale;
            Resources res = app.getBaseContext().getResources();
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }

    // 用于Activity更新语言设置
    public static void updateConfig(ContextThemeWrapper wrapper) {
        if (appLocale != null) {
            Configuration configuration = new Configuration();
            configuration.setLocale(appLocale);
            wrapper.applyOverrideConfiguration(configuration);
        }
    }

Application中设置

    @Override
    public void onCreate() {
        super.onCreate();
        MyAppControl.initAppLocale(this);
        MyAppControl.updateConfig(this, getBaseContext().getResources().getConfiguration());

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        MyAppControl.updateConfig(this, newConfig);
    }

Activity基础类

import android.support.v4.app.FragmentActivity;

import com.rustfisher.util.MyAppControl;

/**
 * 基础类
 * Create on 2019-1-7
 */
public abstract class AbsBaseActivity extends FragmentActivity {

    public AbsBaseActivity() {
        MyAppControl.updateConfig(this); // 设置语言
    }
}

含有WebView的Activity若进行重载语言的操作,加载layout时webView会报错,

Error inflating class android.webkit.WebView