The app I am developing supports two languages: English and Turkish. User can go to Settings
and change the interface to their desired language. The problem I am facing is that sometimes it is working, but some other times it is not.
Here is the code that runs after user selects a language:
private void changeLanguage(LanguageEnum selectedLanguage) {
getPreference().setLanguage(selectedLanguage.getShortName()); // saving `en` or `tr` to pref
// this is a copy paste code I got from SO
Resources resources = getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
config.setLocale(new Locale(selectedLanguage.getShortName().toLowerCase()));
resources.updateConfiguration(config, dm);
activity.recreate();
}
This is my LanguageEnum
:
public enum LanguageEnum {
TR("tr", "Türkçe (TR)"),
EN("en", "English (EN)");
private String shortName;
private String name;
LanguageEnum(String shortName, String name) {
this.shortName = shortName;
this.name = name;
}
// some other methods
}
From another SO post, I found this snipped and put it in my BaseActivity
class:
public abstract class BaseActivity extends BaseManagerActivity {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(updateBaseContextLocale(base));
}
private Context updateBaseContextLocale(Context context) {
String language = PreferenceHelper.getInstance(context).getLanguage();
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
return updateResourcesLocale(context, locale);
}
return updateResourcesLocaleLegacy(context, locale);
}
@TargetApi(Build.VERSION_CODES.N_MR1)
private Context updateResourcesLocale(Context context, Locale locale) {
Configuration configuration = new Configuration(context.getResources().getConfiguration());
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private Context updateResourcesLocaleLegacy(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
I copy pasted from those SO posts because I couldn't find a decent explanation as to how this supporting multiple languages actually work and what exactly needs to be done for them to work properly. I modified it a bit -> just getting the language shortName en
or tr
from the preference, thats it.
Current code has this bug: Switching from one language to another sometimes works perfectly, without any error. I kill the app, restart it, and no problem. Sometimes, it is not switching to the selected language. I checked the preference values and there is no problem, I am getting say tr
from preference, and feeding it to Locale
in the BaseActivity
but everything is in English.
I couldn't find much of information or a solid example on supporting multiple language thing. Even in the Android docs there is no specific code or example. The only thing that is mentioned in the docs that we should have multiple strings.xml
files inside proper values-xx
folders or maybe I overlooked.
I would appreciate any help, thanks.