2

it is a duplicate question but i didn't get a solution from those answers that's why i post this question, am searching for a solution more than a week ..

In Android, Oreo Localisation is not working some times. All strings are displayed in device language only.

 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);


                }

i tried

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/

these answers not give a solution please help me

1 Answers1

1

This code worked for me in the past, It's from an amazing project I'm part of:

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

Maybe you're missing what's in the if statement:

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);
}

Regardless, instead of duplicating the code, call the setLocale() method with the desired local String :)

HedeH
  • 2,869
  • 16
  • 25