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

问题描述 投票:2回答:1

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

在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/

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

android localization android-8.0-oreo
1个回答
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()方法:)

© www.soinside.com 2019 - 2024. All rights reserved.