美文网首页Android开发多语言切换
Android 多语言切换适配到7.0以上(8.0)

Android 多语言切换适配到7.0以上(8.0)

作者: 胡洁_ec66 | 来源:发表于2019-04-11 16:32 被阅读24次

最近公司在做一款app,需要做国际化处理。嗯,那就开始做吧!!!

多语言资源文件

多语言资源文件

切换语言代码

   public void switchLanguage(Locale locale) {
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        config.locale = locale; //
        resources.updateConfiguration(config, dm);
    }

好了,运行app,没问题。
等等!!!在高版本手机上运行怎么不管用?
原来Android7.0以上Configuration将通过LocaleList来管理语言,并且系统切换语言后,系统默认语言可能并不在LocaleList顶部。

好吧,那我们开始爬坑吧。

对7.0以上手机做专门的处理

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            Locale.setDefault(locale);
            return context.createConfigurationContext(config);
        } else {
            config.locale = locale;
        }

用法

在Application中重写attachBaseContext()调用初始化方法
8.0 在 attachBaseContext调用setLocal()返回个 context ,就可以了。

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocalManageUtil.setLocal(base));
    }
   @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 当切换横竖屏 重置语言
        LocalManageUtil.setLocal(getApplicationContext());
    }

然后在BaseActivtiy中重写attachBaseContext()方法

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocalManageUtil.setLocal(base));
    }

具体工具类实现

package base.com.jack.baselibrary.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;
import java.util.Locale;


/**
 * 作者: Jack
 * 描述: 语言切换工具类
 */

public class LocalManageUtil {
    /**
     * 获取选择的语言设置
     * @param context
     * @return
     */
    private static Locale getSetLanguageLocale(Context context) {
        SharedPreferencesUtil.init(context);
        switch (SharedPreferencesUtil.getPrefInt("local",0)) {
            case 0://跟随系统
                return getSystemLocale();
            case 1://英语
                return Locale.ENGLISH;
            case 2://汉语
                return Locale.CHINESE;
            default://默认 汉语
                return Locale.CHINESE;
        }
    }

    /**
     * 设置 本地语言
     *
     * @param context
     * @param select
     */
    public static void saveSelectLanguage(Context context, int select) {
        SharedPreferencesUtil.setPrefInt("local",select);
        setApplicationLanguage(context);
    }


    /**
     * 初始化语言 方法
     *
     * @param context
     */
    public static Context setLocal(Context context) {
        return setApplicationLanguage(context);
    }

    /**
     * 设置语言类型
     */
    public static Context setApplicationLanguage(Context context) {

        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        Locale locale = getSetLanguageLocale(context);//获取sp里面保存的语言

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            Locale.setDefault(locale);
            return context.createConfigurationContext(config);
        } else {
            config.locale = locale;
        }
        resources.updateConfiguration(config, dm);
        return context;
    }

    /**
     * 获取App的locale
     *
     * @return Locale对象
     */
    public static Locale getAppLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }

    /**
     * 获取系统local
     *
     * @return
     */
    public static Locale getSystemLocale() {
        Locale locale = Resources.getSystem().getConfiguration().locale;
        return locale;
    }

    /**
     * 获取本地保存的语言
     *
     * @param context
     * @return
     */
    public static String getLocalSaveLanguage(Context context) {
        Locale locale = getSetLanguageLocale(context);
        String language = locale.getLanguage();
        if (language.equals("zh")) {
            language = "zh-CN";
        } else if (language.equals("en")) {
            language = "en";
        } else if (language.equals("ja")) {
            language = "ja";
        }
        return language;
    }

}

最后在需要调用语言切换的地方调用就可以了

          switch (view.getId()) {
            case R.id.chinese:
                LocalManageUtil.saveSelectLanguage(this,2);
                break;
            case R.id.english:
                LocalManageUtil.saveSelectLanguage(this,1);
                break;
          }

可能在某些手机上还是会有问题,因为测试机有限只能做到现在的程度了。

参考https://blog.csdn.net/sinat_32961877/article/details/85007148

相关文章

网友评论

    本文标题:Android 多语言切换适配到7.0以上(8.0)

    本文链接:https://www.haomeiwen.com/subject/iaztwqtx.html