1

I am currently working on an Android app that needs to support languages English and Arabic, i am using this code to switch from a language to the other.

String languageToLoad; // your language
    if (languageSwitch.isChecked()) {
        languageToLoad = "ar";//arabic
    } else {
        languageToLoad = "en";//english
    }

    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;

    // store it in the cache for any further use
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
    SharedPreferencesUtil.putString(this, "language", languageToLoad);

It was working fine, i've made 2 layouts folders, a normal one and a layout-ar. It was working correctly but sometimes it gets messed up and instead of showing the Arabic one, it shows the English layout and the app continues on running in the English mode only.

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
Kingofkech
  • 129
  • 9

2 Answers2

1

At the end you should restart you activity. Try this.

 public void updateActivity() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
  • this is what i use but some layouts just don't migrate to the good layout they're still stuck on the english layouts – Kingofkech Jul 02 '18 at 15:42
  • might be better to `Intent intent = getIntent()`, so the activity will be restarted with the same extras bundle. – petey Jul 02 '18 at 18:10
0

It turns out that the webview is the one responsible for the problem, once the webview is generated it deletes all the overrided local data and inserts the one from the device,in order to correct the problem you need to follow the steps found in this link.

Kingofkech
  • 129
  • 9