首页 > 解决方案 > 区域设置未在发布包中更新

问题描述

语言环境在发布包中没有更新,但它在调试 apk 中工作。我正在使用 targetSdkVersion 29 和 androidx.appcompat:appcompat:1.3.0-alpha02

在 MainActivity

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.updateResources(base));
}


public class LocaleManager {

public static String TAG = "LocaleManager";
public static String LOCALE_HI = "hi_IN";
public static String LOCALE_EN = "en_US";
private static String lan_str;
private static String country_str;
public static void setlan(String str)
{
    String val[] = str.split("_");
    LocaleManager.lan_str = val[0];
    LocaleManager.country_str = val[1];
}

public static String getlanguage()
{
    return LocaleManager.lan_str+"_"+LocaleManager.country_str;
}

 public static Context updateResources(Context context) {

    if(lan_str == null || country_str == null)
        return context;

    Locale locale = new Locale(lan_str,country_str);
    Locale.setDefault(locale);
    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        config.setLocale(locale);
        config.locale = locale;
    } else {
        config.locale = locale;
    }
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
        context = context.createConfigurationContext(config);
    } else {
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}

}

更改语言后,我正在重新启动 MainActivity,但语言环境未更新。

标签: androidsetlocale

解决方案


最后我得到了定位的解决方案,在 app build.gradle 的 android 块中添加了这个

bundle {
    language {
        enableSplit = false
    }
}

引用自https://www.mmbyte.com/article/55895.html。谢谢


推荐阅读