不重啓應用,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

相關文章
相關標籤/搜索