美文网首页
万能国际化(以及出现各种语言混合问题解决)

万能国际化(以及出现各种语言混合问题解决)

作者: 禄子_c79b | 来源:发表于2019-05-30 12:11 被阅读0次

private static final String TAG = "LocalManageUtil";

/**
 * 获取系统的locale
 *
 * @return Locale对象
 */
public static Locale getSystemLocale(Context context) {
    return SPUtil.getInstance(context).getSystemCurrentLocal();
}

// public static String getSelectLanguage(Context context) {
//// switch (SPUtil.getInstance(context).getSelectLanguage()) {
//// case 1:
//// return context.getString(R.string.chinese);
////// case 2:
////// return context.getString(R.string.english);
//// case 3:
//// return context.getString(R.string.english);
////
// default:
// return context.getString(R.string.english);
//// }
// }

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

// Configuration config = context.getResources().getConfiguration();
// config.locale = new Locale("in");
switch (SPUtil.getInstance(context).getSelectLanguage()) {
case 0:
return getSystemLocale(context);//getSelectLanguage()选择0不会第一次默认选择默认语言不会随系统改变注意事项之前是0及系统语言,这里有问题不能设置系统的
// new Locale("vi")
case 1:
// return Locale.CHINA;
return new Locale("en");
case 2:
// return Locale.TAIWAN;
return new Locale("in");
case 3:
// return Locale.ENGLISH;
return new Locale("zh");
default:
// return Locale.ENGLISH;
return new Locale("in");
}
}

public static void saveSelectLanguage(Context context, int select) {
    SPUtil.getInstance(context).saveLanguage(select);
    setApplicationLanguage(context);
}

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;
}

/**
 * 设置语言类型
 */
public static void setApplicationLanguage(Context context) {
    Resources resources = context.getApplicationContext().getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    Locale locale = getSetLanguageLocale(context);
    config.locale = locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.getApplicationContext().createConfigurationContext(config);
        Locale.setDefault(locale);
    }
    resources.updateConfiguration(config, dm);
}

public static void saveSystemCurrentLanguage(Context context) {
    Locale locale;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        locale = LocaleList.getDefault().get(0);
    } else {
        locale = Locale.getDefault();
    }
    Log.d(TAG, locale.getLanguage());
    SPUtil.getInstance(context).setSystemCurrentLocal(locale);
}

public static void onConfigurationChanged(Context context){
    saveSystemCurrentLanguage(context);
    setLocal(context);
    setApplicationLanguage(context);
}

二:设置语言后要跳转的activity加入(这里拿VipLoginActivity登录作为重启activity)
public static void reStart(Context context) {
Intent intent = new Intent(context, VipLoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
二:所有activity基类里都加入(这里是关键,一定要保证所有activity都加入一下代码,如果有activity有没添加,一旦该activity处于栈里就会出现语言错乱混合)

//国际化
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LocalManageUtil.setLocal(newBase));
}
@Override
public Resources getResources() {
Resources res = super.getResources();
Configuration config = new Configuration();
config.setToDefaults();
res.updateConfiguration(config, res.getDisplayMetrics());
return res;
}

相关文章

网友评论

      本文标题:万能国际化(以及出现各种语言混合问题解决)

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