0

I am trying to change app localization at runtime it working correctly but when clear app from memory and reopen it again I find that resources like strings.xml and styles.xml take the default mobile language!

Mina George
  • 184
  • 12

1 Answers1

0

See my method to localize the app:

I created an abstract class like below:

public abstract class BaseLocalization extends AppCompatActivity {
    SharedPreferences preferences;
    Constants constants;
    String currentLocale = "en";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        constants = new Constants();
        preferences = getSharedPreferences(constants.getPreferences(), MODE_PRIVATE);
        currentLocale = preferences.getString(constants.getLocale(), "en");
        setLocale(currentLocale);

    }

    public void setLocale(String lang) {
        Locale myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
    }
}

After doing this, I made all the classes which extended AppCompatActivity extend this class. And saved the locale in SharedPreferences. And the locale is persisted until the app data is cleared/uninstalled.

Vedprakash Wagh
  • 3,595
  • 3
  • 12
  • 33