【问题标题】:Oreo Localisation sometimes not working奥利奥本地化有时不起作用
【发布时间】:2018-05-16 11:23:17
【问题描述】:

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

在 Android 中,奥利奥本地化有时无法正常工作。所有字符串仅以设备语言显示。

 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/

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

【问题讨论】:

    标签: android localization android-8.0-oreo


    【解决方案1】:

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

    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() 方法:)

    【讨论】:

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