Android 语言切换

一、实现思路

  • 在application 的 attachBaseContext设置当前设置的语言Local
  • 在application 的onConfigurationChanged(改变系统语言时会调用到)设置当前的语言Local
  • 在 Activity 的attachBaseContext设置当前设置的语言Local,所以一般这里是创建BaseActivity来方便统一改变
  • 在 service 的attachBaseContext设置当前设置的语言Local

二、用sp中保存设置

public class SPUtil {

    private final String SP_NAME = "language_setting";
    private final String TAG_LANGUAGE = "language_select";
    private static volatile SPUtil instance;

    private final SharedPreferences mSharedPreferences;

    public SPUtil(Context context) {
        mSharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }


    public void saveLanguage(int select) {
        SharedPreferences.Editor edit = mSharedPreferences.edit();
        edit.putInt(TAG_LANGUAGE, select);
        edit.commit();
    }

    public int getSelectLanguage() {
        return mSharedPreferences.getInt(TAG_LANGUAGE, 0);
    }

    public static SPUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (SPUtil.class) {
                if (instance == null) {
                    instance = new SPUtil(context);
                }
            }
        }
        return instance;
    }
}

三、获取语言

/**
     * 获取选择的语言设置
     *
     * @param context
     * @return
     */
    public static Locale getSetLanguageLocale(Context context) {

        switch (SPUtil.getInstance(context).getSelectLanguage()) {
            case 0:
                return getSystemLocale(context);
            case 1:
                return Locale.CHINA;
            case 2:
                return Locale.TAIWAN;
            case 3:
            default:
                return Locale.ENGLISH;
        }
    }

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

四、改变语言

public static Context setLocal(Context context) {
        return updateResources(context, getSetLanguageLocale(context));
    }

private static Context updateResources(Context context, Locale locale) {
     Locale.setDefault(locale);

     Resources res = context.getResources();
     Configuration config = new Configuration(res.getConfiguration());
     if (Build.VERSION.SDK_INT >= 17) {
         config.setLocale(locale);
         context = context.createConfigurationContext(config);
     } else {
         config.locale = locale;
         res.updateConfiguration(config, res.getDisplayMetrics());
     }
     return context;
 }

五、在相应的地方调用设置语言

(1)、Application

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

   @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocalManageUtil.setLocal(context);
    }

(2)、BaseActivity

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

(3)、service

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

(4)、切换语言

    private void selectLanguage(int select) {
        LocalManageUtil.saveSelectLanguage(this, select);
        //重启APP到主页面
        MainActivity.reStart(this);
    }

你可能感兴趣的:(技术分享,android,移动开发)