不重启应用,android APP内部国际化

当前android的语言切换,不少项目中都是切换之后须要重启项目,这样的体验是很差的,如何实现当前界面切换,当即能看到效果?android

目前项目中只支持中文和英文,具体实现:app

在BaseActivity中:ui

在SharedPreUtils文件中:对象

public static Context selectLanguage(Context context, String language) {
    Context updateContext;
    //设置语言类型
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        updateContext =  createConfigurationResources(context, language);
    } else {
        applyLanguage(context, language);
        updateContext =  context;
    }
    //保存设置语言的类型
    SharedPreUtils.putString(context,Constant.LANGUAGE, language);
    return updateContext;
}

@TargetApi(Build.VERSION_CODES.N)
private static Context createConfigurationResources(Context context, String language) {
    //设置语言类型
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    Locale locale = null;
    switch (language) {
        case "en":
            locale = Locale.ENGLISH;
            break;
        case "zh":
            locale = Locale.SIMPLIFIED_CHINESE;
            break;
        default:
            locale = Locale.getDefault();
            break;
    }
    configuration.setLocale(locale);
    return context.createConfigurationContext(configuration);
}

private static void applyLanguage(Context context, String language) {
    //设置语言类型
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    Locale locale = null;
    switch (language) {
        case "en":
            locale = Locale.ENGLISH;
            break;
        case "zh":
            locale = Locale.SIMPLIFIED_CHINESE;
            break;
        default:
            locale = Locale.getDefault();
            break;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        // apply locale
        configuration.setLocale(locale);
    } else {
        // updateConfiguration
        configuration.locale = locale;
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
    }
}

public static Context updateLanguage(Context context) {
    String curLanguage = getString(context, Constant.LANGUAGE);
    if (null == curLanguage || TextUtils.isEmpty(curLanguage)) {
        curLanguage = Constant.LANGUAGE_ZH;
    }
    return selectLanguage(context, curLanguage);
}
public static boolean switchLanguage(Context context, String value) {
    String curLanguage = getString(context, Constant.LANGUAGE);
    if (value.equals(curLanguage)) {
        return false;
    }
    if (null == curLanguage || TextUtils.isEmpty(curLanguage)) {
        putString(context, Constant.LANGUAGE, Constant.LANGUAGE_EN);
    } else {
        if (curLanguage.equals(Constant.LANGUAGE_EN)) {
            putString(context, Constant.LANGUAGE, Constant.LANGUAGE_ZH);
        } else {
            putString(context, Constant.LANGUAGE, Constant.LANGUAGE_EN);
        }
    }
    selectLanguage(context, getString(context, Constant.LANGUAGE));
    return true;
}

再android7以前,能够直接使用resources.updateConfiguration(configuration, dm);完成语言的切换,可是以后咱们须要从新替换Context,把Context替换成设置了指定语言的Context对象,咱们能够采起如下方案,context.createConfigurationContext(configuration);blog

最后须要注意的是,在切换语言的地方须要调用首先调用switchLanguage,而后调用activity的recreate方法从新构造activity(固然,若是不怕麻烦,也能够先finish,而后start当前 ),这个操做最好是放在baseActvitiy中,这样不须要重启应用也能完成语言的切换。get

相关文章
相关标签/搜索