【问题标题】:Changing the language manually not working on Samsung devices手动更改语言不适用于三星设备
【发布时间】:2020-03-13 03:05:46
【问题描述】:

我在手动更改应用程序语言时遇到问题,在应用程序中,我让用户能够将应用程序的语言更改为他们喜欢的语言,下面的代码即使在 Android 中也能正常工作(Pixel 3 Emulator ),但由于某种原因,它不适用于所有三星设备

            Context context = LocaleUtils.setLocale(getApplicationContext(), languageCode);
            Resources resources = context.getResources();
            Locale myLocale = new Locale(languageCode);
            DisplayMetrics dm = resources.getDisplayMetrics();
            Configuration conf = resources.getConfiguration();
            conf.locale = myLocale;
            resources.updateConfiguration(conf, dm);
            Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(
                    getBaseContext().getPackageName());
            if (intent != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }

应用类:

 @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        LocaleUtils.onAttach(base, Locale.getDefault().getLanguage());
        MultiDex.install(this);
   }

在每个活动上:

  @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(LocaleUtils.onAttach(newBase)));
    }

【问题讨论】:

  • 我也有同样的问题。我尝试了多个库但没有成功。我终于设法拿到了三星设备,并调试了在某些情况下在活动中调用 getResources() 会返回将其配置设置回原始语言环境的资源。似乎在某些情况下,其他东西会覆盖活动中的语言环境。 PS:我在每个活动的 onCreate 方法中覆盖了语言环境。
  • 我刚刚设法为我拥有的设备修复了它。我最终使用了这个解决方案stackoverflow.com/a/59370534/3296947,并在调用 super() 之后立即在活动的 attachBaseContext 中调用它。有关此主题的更多信息:stackoverflow.com/questions/55265834/…

标签: java android locale android-10.0


【解决方案1】:

即使在 Android 10 之前,我也很难在三星设备上进行动态区域设置更改。
现在可能有更好的解决方案,
但当时除了你所做的,
我最终通过以下方式通过资源标识符检索所有字符串:

public static String getStringByIdentifier(final Context context, final String stringResIdName, final boolean forceRefresh) {
        final Resources res = getResources(context, forceRefresh);
        String result;
        try {
            result = res.getString(res.getIdentifier(stringResIdName, "string",
                    context.getPackageName()));
        } catch (final Resources.NotFoundException e) {
            result = stringResIdName; // TODO or here you may throw an Exception and handle it accordingly. 
        }
        return result;
    }

public static String getStringByIdentifier(final Context context, final String stringResIdName) {
        return getStringByIdentifier(context, stringResIdName, false);
    }

 private static Resources getResources(final Context context, final boolean refreshLocale) {
        if (!refreshLocale) {
            return context.getResources();
        } else {
            final Configuration configuration = new Configuration(context.getResources().getConfiguration());
            configuration.setLocale(Locale.getDefault());
            return context.createConfigurationContext(configuration).getResources();
        }
    }

所以你必须用下一种方式设置文本:

textView.setText(AndroidUtils.getStringByIdentifier(context, "string_res_name"));

对应的字符串资源在哪里:

<string name="string_res_name">Some string</string>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多