安卓 android 多语言工具类

/**
 * @author xt on 2020/1/6 13:09
 * 英文增加value-en文件夹
 * 中文简体增加values-zh文件夹
 * <p>
 * 清单文件中错误配置:
 * <activity
 * android:name=".activity.dvr.FactoryResetActivity"
 * android:configChanges="locale|orientation|keyboard|layoutDirection|screenSize"
 * android:screenOrientation="portrait" />
 * 不应该配置locale:
 * <activity
 * android:name=".activity.dvr.FactoryResetActivity"
 * android:configChanges="orientation|keyboard|layoutDirection|screenSize"
 * android:screenOrientation="portrait" />
 */
public class MyLanguageUtils {
    public static final String LANGUAGE_TYPE_FOLLOWING_SYSTEM   = "FOLLOWING_SYSTEM";
    public static final String LANGUAGE_TYPE_SIMPLIFIED_CHINESE = "SIMPLIFIED_CHINESE";
    public static final String LANGUAGE_TYPE_ENGLISH            = "ENGLISH";

    public static Context updateLanguage(Context context) {
        Locale locale = getAppCurrentLocale(context);

        if (locale == null) {
            return context;
        }

        //设置值的代码,高版本的代码和过时的代码一起执行
        update(context, locale);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            context = context.createConfigurationContext(context.getResources().getConfiguration());
            update(context, locale);
        }

        return context;
    }

    /**
     * activity onConfigurationChanged方法中调用
     *
     * @param activity
     * @param newConfig
     */
    public static void onActivityConfigurationChanged(Activity activity, Configuration newConfig) {
        activity.getResources().getConfiguration().orientation = newConfig.orientation;
        activity.getResources().getDisplayMetrics().setTo(activity.getResources().getDisplayMetrics());

        activity.getApplication().getResources().getConfiguration().orientation = newConfig.orientation;
        activity.getApplication().getResources().getDisplayMetrics().setTo(activity.getResources().getDisplayMetrics());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            activity.getResources().getConfiguration().setLocales(newConfig.getLocales());
        }
        activity.getResources().getConfiguration().locale = newConfig.locale;

        updateLanguage(activity);
    }

    /**
     * application onConfigurationChanged方法中调用
     *
     * @param application
     * @param newConfig
     */
    public static void onAppConfigurationChanged(Application application, Configuration newConfig) {
        application.getResources().getConfiguration().orientation = newConfig.orientation;
        application.getResources().getDisplayMetrics().setTo(application.getResources().getDisplayMetrics());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            application.getResources().getConfiguration().setLocales(newConfig.getLocales());
        }
        application.getResources().getConfiguration().locale = newConfig.locale;

        updateLanguage(application);
    }

    /**
     * @param activity
     * @param languageType
     */
    public static void saveLanguage(Activity activity, String languageType) {
        SharedPreferences        preferences = activity.getSharedPreferences("language", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor      = preferences.edit();
        editor.putString("language", languageType);
        editor.commit();
        {
            for (Activity item : ActivityUtils.getActivityList()) {
                MyLanguageUtils.updateLanguage(item);
            }
            MyLanguageUtils.updateLanguage(Utils.getApp());
            MainActivity.start(activity);
        }
    }

    public static String getCurrentLanguageType(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("language", Context.MODE_PRIVATE);
        return preferences.getString("language", LANGUAGE_TYPE_FOLLOWING_SYSTEM);
    }

    /**
     * 是否是简体中文
     *
     * @param context
     * @return
     */
    public static boolean isAppCurrentLocaleSimplifiedChinese(Context context) {
        Locale currentLocale = getAppCurrentLocale(context);
        return TextUtils.equals(currentLocale.getLanguage(), "zh") && TextUtils.equals(currentLocale.getCountry(), "CN");
    }

    private static Locale getSystemLocale() {
        return ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);
    }

    /**
     * 中文分简体和繁体,英文不分是UK、US、CANADA,目前只支持简体中文和英文
     *
     * @param context
     * @return
     */
    private static Locale getAppCurrentLocale(Context context) {
        String language = getCurrentLanguageType(context);
        if (TextUtils.equals(language, LANGUAGE_TYPE_SIMPLIFIED_CHINESE)) {
            return Locale.SIMPLIFIED_CHINESE;
        } else if (TextUtils.equals(language, LANGUAGE_TYPE_ENGLISH)) {
            return Locale.ENGLISH;
        } else {
            return getSystemLocale();
        }
    }

    private static void update(Context context, Locale locale) {
        Resources      resources      = context.getResources();
        Configuration  configuration  = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        }
        resources.updateConfiguration(configuration, displayMetrics);
    }
}

 

调用的位置:

1、application onConfigurationChanged方法中调用MyLanguageUtils.onAppConfigurationChanged

2、application attachBaseContext方法中调用MyLanguageUtils.updateLanguage

3、BaseActivity attachBaseContext方法中调用MyLanguageUtils.updateLanguage

4、BaseActivity onConfigurationChanged方法中调用MyLanguageUtils.onActivityConfigurationChanged

5、在语言切换界面调用MyLanguageUtils.saveLanguage

 

需要在MainActivity的onCreate方法中调用一下代码

for (Activity activity : ActivityUtils.getActivityList()) {
    if (!activity.equals(this)) {
        activity.finish();
    }
}

 

引用的第三方库:com.blankj.utilcode

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值