【问题标题】:When changing Locale, getResources from Application not updated. Only Activity getResources is updated correctly更改区域设置时,来自应用程序的 getResources 未更新。只有 Activity getResources 正确更新
【发布时间】:2019-12-25 14:09:04
【问题描述】:

该应用程序具有多语言支持。但是我们遇到了Resources 刷新应用程序上下文的问题。

目前,我的ViewModels 正在扩展AndroidViewModel,以便我们可以访问ViewModels 中的应用程序实例。但问题是 应用程序资源 在区域设置更改后不会立即刷新。

所以,如果我更改语言环境并返回到我的 LoginActivity,以下代码会给出不同的输出

    String testText = getString(R.string.enter_email);
    Timber.e("-- From Activity --");
    Timber.e(testText);

    Timber.e("-- From Application--");
    testText = getApplication().getString(R.string.enter_email);
    Timber.e(testText);

这个sn-p的Logcat输出如下

E/LoginActivity: -- From Activity --
E/LoginActivity: الرجاء إدخال البريد الإلكتروني
E/LoginActivity: -- From Application--
E/LoginActivity: Please enter your email

我正在使用以下 sn-p 进行语言环境更新:

public static Context setLocale(Context context, String language) {
    saveLocale(context, language);
    CountryUtils.getDefaultCountryISO(context));
    Locale locale = new Locale(language, CountryUtils.getDefaultCountryISO(context));
    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;
}

我已按照bloganswer 中提到的所有步骤进行操作。

我需要了解的是,为什么我们在getApplication().getString()this.getString() 之间有不同的资源?

【问题讨论】:

    标签: android android-resources android-context


    【解决方案1】:

    在您的活动中添加以下方法

     override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(PbContext.wrap(newBase, getmAppLanguage()))
    }
    

    请在下面找到扩展自定义上下文:

    public class PbContext extends ContextWrapper {
    
    public PbContext(Context base) {
        super(base);
    }
    
    @SuppressWarnings("deprecation")
    public static PbContext wrap(Context context, String language) {
        Configuration config = context.getResources().getConfiguration();
    
        if (language != null) {
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            config.setLayoutDirection(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            } else {
                setSystemLocaleLegacy(config, locale);
            }
    
            context = context.createConfigurationContext(config);
    
        }
        //Updating application
        SampleApplication.setAppConfig(context);
        return new PbContext(context);
    }
    
    @SuppressWarnings("deprecation")
    public static Locale getSystemLocaleLegacy(Configuration config){
        return config.locale;
    }
    
    @TargetApi(Build.VERSION_CODES.N)
    public static Locale getSystemLocale(Configuration config){
        return config.getLocales().get(0);
    }
    
    @SuppressWarnings("deprecation")
    public static void setSystemLocaleLegacy(Configuration config, Locale locale){
        config.locale = locale;
    }
    
    @TargetApi(Build.VERSION_CODES.N)
    public static void setSystemLocale(Configuration config, Locale locale){
        config.setLocale(locale);
    }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多