0

I have a drop down, user can either choose English or Spanish language.

The issue is that on language changed, it converts date, time, progress dialog text to spanish or english, but the strings that i defined are not converted to the specified language.

Although they do convert after 1 or 2 clicks, so when i click on ES it won't change strings to ES, but then i click on EN, it will work and convert all string to ES, which is not the desired result.

When user taps on any language i execute following code.

languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

 public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        selectedLanguage=  getResources().getStringArray(R.array.language_array)[i];
        loadLanguage(selectedLanguage.toLowerCase());
 }

The load language method perfoms these operations.

    public void loadLanguage(String language){

        String languageToLoad  = language;
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);

        Configuration config = new Configuration();
        config.setLocale(locale);
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());


        recreate(); 
 }

At each activity i have added following code.

  @Override
    protected void attachBaseContext(Context newBase) {
        Locale locale = new Locale(LanguagePreference.getInstance().getUserLanguage(newBase));
        Context context = ContextWrapper.wrap(newBase, locale);
        super.attachBaseContext(context);
    }

Following is my Context Wrapper class.

  public static ContextWrapper wrap(Context context, Locale newLocale) {

        Resources res = context.getResources();
        Configuration configuration = res.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(newLocale);

            LocaleList localeList = new LocaleList(newLocale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);

            context = context.createConfigurationContext(configuration);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(newLocale);
            context = context.createConfigurationContext(configuration);

        } else {
            configuration.locale = newLocale;
            res.updateConfiguration(configuration, res.getDisplayMetrics());
        }

        return new ContextWrapper(context);
    }}
dev90
  • 7,187
  • 15
  • 80
  • 153
  • have u check this https://stackoverflow.com/a/52270630/7666442 – AskNilesh Jan 04 '19 at 11:26
  • are you sure the respective es strings are available against those keys ? – Sara Tirmizi Jan 04 '19 at 11:30
  • @SaraTirmizi : Yes they are, and they actually work sometimes, but if i start changing language frequently then they stop working, or sometimes they does not change language at all. – dev90 Jan 04 '19 at 11:37
  • @NileshRathod: Yes i checked that answer – dev90 Jan 04 '19 at 11:37
  • @Kirmani88 this is the problem , changing basecontext takes time , rapidly changing may cause this issue, make sure to add delay may be 500 after changing language , this will resolve the issue – Sara Tirmizi Jan 04 '19 at 11:40
  • @SaraTirmizi : Thanks, i have added a delay, but still its updating strings sometimes and does not update any few times. – dev90 Jan 04 '19 at 12:02
  • @NileshRathod: If i click on any other activities it loads and works fine, and if i revisit the base activity it also loads fine, the issue only occurs if i stay on the same activity. – dev90 Jan 04 '19 at 12:50
  • @Kirmani88 after changing language did u restart the activitys – AskNilesh Jan 04 '19 at 12:51
  • @SaraTirmizi : If i click on any other activities it loads and works fine, and if i revisit the base activity it also loads fine, the issue only occurs if i stay on the same activity, it just reloads the activity, updates date string, but does not update strings that are defined in my strings.xml file. – dev90 Jan 04 '19 at 12:51
  • @NileshRathod : Yes it did,I execute `recreate(); ` – dev90 Jan 04 '19 at 12:52
  • @Kirmani88 may that the issue try to restart your activity like this https://stackoverflow.com/a/3974828/7666442 – AskNilesh Jan 04 '19 at 12:54
  • @NileshRathod: tired that, but the issue is still there. One thing i noticed that, it updates dates and other strings that are part of system, but does not updates strings defined in my app.When i revist aactivity, all strings are updated. – dev90 Jan 04 '19 at 13:45

0 Answers0