(I've added an update on the bottom of the post)
I am currently working on a project that will support multiple language. I have written all strings for all languages.
I use BaseActivity, Application class, and LocaleHelper to manage the localization on my app. And then I found this behavior on my app. After I setLocale to another language, Strings in some activities are changed and correct, but NOT in MainActivity even though they have some same strings. I tried to restart app and it's still not working.
I also have another activity which some of the words in activity got translated but all words inside a fragment and a recyclerview didn't get translated at all.
I still couldn't find why it's not translated properly. Can someone help me??
Here's some snippet:
Application class
@Override
public void onCreate() {
super.onCreate();
LocaleUtil.setLocale(new Locale(LocaleUtil.with(this).getPreference()));
LocaleUtil.updateConfig(this,getBaseContext().getResources().getConfiguration());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtil.setLocale(new Locale(LocaleUtil.with(this).getPreference()));
LocaleUtil.updateConfig(this, newConfig);
}
BaseActivity
public BaseActivity() {
LocaleUtil.updateConfig(this);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
initLocale();
super.onCreate(savedInstanceState);
}
public void initLocale(){
localeUtil = new LocaleUtil(this);
localeUtil.setListener(this);
localeUtil.setLanguageByCode(localeUtil.getPreference());
}
Please help me! THanks!!
(UPDATE)
I found out that I have to setLocale again whenever I want to access Strings.xml for any views in a recyclerview and a fragment. This is totally inconvenient and I'm worried about the performance. I would love to hear a better advice to change language.