0

I am using Restring library to load localized strings on my app. And I am able to load localized strings from the api and display it on the app, with out any issues. But the pitfall is every time the language is switched, I have to restart the app, then only the language is updated on the app. But I need to achieve it without restarting the app. Any help is appreciated. I already referred some links, which I am providing below,

https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758

https://medium.com/@hamidgh/dynamically-change-bundled-strings-a24b97bfd306

Change app language programmatically in Android

Note: Restring uses SharedPreferences as the String Repository, so when ever getString(id) method is invoked, it'll provide the string matching the id from its SharedPrefernces repository.

Gurupriyan
  • 51
  • 9

2 Answers2

1

You can use a new version of Restring (based on the one you are using) from here: https://github.com/B3nedikt/restring

It includes a method to update your activity without any restart. Restring.reword(rootView) will look into all child views of "rootView" and update strings

   Restring.setLocale(Locale.FRENCH);

   // The layout containing the views you want to localize

   final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

   Restring.reword(rootView);

It also explains how to make sure each activity will get the desired strings when created: in all activities, add:

@Override
protected void attachBaseContext(Context newBase) {
  super.attachBaseContext(ViewPumpContextWrapper.wrap(Restring.wrapContext(newBase)));    
}

@Override
public Resources getResources() {
  return Restring.wrapContext(getBaseContext()).getResources();    
}
Sylvain
  • 26
  • 4
1

I would recommend applying View-Model in your android app architecture, A View-Model's major role is to survive configuration changes (which includes but are not limited to orientation changes from landscape to portrait and vice-versa, surviving drastic changes in app's performance stats like power-drain and many more) so you will not need to restart yoour app + additionally use live-Data which is Activity/Fragment aware so it will only update your data of the app when it is in foreground.

There are many YouTube videos specifically teaching these things, I would recommend watching "coding in flow" YouTube channel's MVVM architecture.

It will definitely give you an idea how to continue with your app, it is only 10 videos long but for your requirement first 5 are enough.