android - 奥利奥本地化有时不起作用

标签 android localization android-8.0-oreo

这是一个重复的问题,但我没有从这些答案中得到解决方案,这就是为什么我发布这个问题,我正在寻找一个多星期的解决方案..

在 Android 中,Oreo Localization 有时无法正常工作。所有字符串仅以设备语言显示。

 if (languagecode.equals("1")) {
                    Resources res = getApplicationContext().getResources();
                    DisplayMetrics dm = res.getDisplayMetrics();
                    android.content.res.Configuration conf = 
                    res.getConfiguration();
                    conf.locale = new Locale("ml");
                    res.updateConfiguration(conf, dm);

                    txt_details.setText(R.string.card_det);
                    txt_no.setText(R.string.card_number);


                }
 if (languagecode.equals("2")) {
                    Resources res = getApplicationContext().getResources();
                    DisplayMetrics dm = res.getDisplayMetrics();
                    android.content.res.Configuration conf = 
                    res.getConfiguration();
                    conf.locale = new Locale("ta");
                    res.updateConfiguration(conf, dm);

                    txt_details.setText(R.string.card_det);
                    txt_no.setText(R.string.card_number);


                }

我试过了

Android N change language programmatically

In android Oreo localization is not working

How to change Android O / Oreo / api 26 app language

https://www.reddit.com/r/androiddev/comments/8b2rol/solution_for_locale_language_change_not_working/

这些答案没有给出解决方案请帮助我

最佳答案

这段代码过去对我有用,它来 self 参与的一个了不起的项目:

https://github.com/nu-art/cyborg-core/blob/92e2e6d9889be48244918eb85f54e9a79a14bb9e/src/main/java/com/nu/art/cyborg/core/modules/LocaleModule.java

也许您遗漏了 if 语句中的内容:

public void setLocale(String localeString) {
  Resources res = getResources();
  Configuration conf = res.getConfiguration();
  Locale locale = new Locale(localeString);
  Locale.setDefault(locale);
  if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
    conf.setLocale(locale);
    getApplicationContext().createConfigurationContext(conf);
  }

  DisplayMetrics dm = res.getDisplayMetrics();
  if (VERSION.SDK_INT >= VERSION_CODES.N) {
    conf.setLocales(new LocaleList(locale));
  } else {
    conf.locale = locale;
  }
  res.updateConfiguration(conf, dm);
}

无论如何,不​​要复制代码,而是使用所需的本地字符串调用 setLocale() 方法 :)

关于android - 奥利奥本地化有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50369608/

相关文章:

android - React native null 不是 Android 上的对象(评估 '_scrollView.scrollTo' )

javascript - 基于多个 ScrollView(s) 为单个 View 设置动画

java - Servlet 对 Android 设备的响应

objective-c - 强制将特定本地化用于目标

ios - Xcode 删除特定于语言的 Storyboard iOS

.net - 覆盖 .net 中的默认日期分隔符

android - Android Oreo运行后台执行限制的疑惑

android - 如何在 Android Oreo 中管理来自未知来源的安装?

android - 从 Android StorageManager 获得的存储 UUID 无效?

Android - Activity onStart