0

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.

amira
  • 416
  • 1
  • 7
  • 24
  • Do you need to change locale at runtime without restarting the app? – Nicola Gallazzi Dec 24 '20 at 09:33
  • Does this answer your question? [Change app language programmatically in Android](https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android) – Nicola Gallazzi Dec 24 '20 at 09:36
  • Is it systematically not working or just randomly? Can you explain in more detail what "not working" means in your case. – laalto Dec 24 '20 at 09:38
  • @NicolaGallazzi I came across with the link you gave me earlier, and no, there is no need for me NOT to restart the app. As you can see in the first snipped I am restarting the app using activity.recreate(). (I couldn't fix the previous comment, deleted it) – amira Dec 24 '20 at 09:53
  • @laalto I don't think it is systematic. At first everything is working like a charm, I am switching from one to another, and back - No problem. But when I KEEP goING back and forth between the two, at one point, I think after the 3rd or 4th attempt it is NOT loading newly selected language. (sorry, I left typos everywhere in the previous comment) – amira Dec 24 '20 at 09:54

0 Answers0