Android App Localization does not change language

Viewed 37

I want to enable user to pick a language between English and Swahili once they open the app for the first time. I have a values-sw-rTZ directory where I have put in swahili versions of my strings.

I have created an activity where they can change the language and Using a ContextWrapper I set the Locale to "sw-rTZ". I put the locale in shared preferences and then I override a function called attachBaseContext in each activity where I want translations to occur by calling super.attachBaseContext(MyContextWrapper.wrap(newBase, language));

I know this works because when I call getResources().getConfiguration().locale; I get the locale I set using contextWrapper. I also know that my Swahili strings in values-sw-rTZ work because when I view the layout of the activity and preview locale I can see them as below: enter image description here

In gradle, I have added the following line:

defaultConfig{
 ....
resConfigs "en", "sw"
}

However, when I run the app on my device, the language does not change. What could be the problem?

Below is my code:

ContextWrapper:

public class MyContextWrapper extends ContextWrapper {

    public MyContextWrapper(Context base) {
        super(base);
    }

    @SuppressWarnings("deprecation")
    public static ContextWrapper wrap(Context context, String language) {
        Configuration config = context.getResources().getConfiguration();
        Locale sysLocale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            sysLocale = getSystemLocale(config);
        } else {
            sysLocale = getSystemLocaleLegacy(config);
        }
        if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
                context.createConfigurationContext(config);
            }
            else {
                setSystemLocaleLegacy(config, locale);
            }
            context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
        }
        return new MyContextWrapper(context);
    }

    @SuppressWarnings("deprecation")
    public static Locale getSystemLocaleLegacy(Configuration config) {
        return config.locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static Locale getSystemLocale(Configuration config) {
        return config.getLocales().get(0);
    }

    @SuppressWarnings("deprecation")
    public static void setSystemLocaleLegacy(Configuration config, Locale locale) {
        config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static void setSystemLocale(Configuration config, Locale locale) {
        config.setLocale(locale);
    }
}

AttachBaseContext in every activity

   @Override
    protected void attachBaseContext(Context newBase) {
        //fetch from shared preference also save to the same when applying. default is English
        //Load localeToSwitchTo from shared Preferences
        SharedPreferences sharedPreferences = newBase.getSharedPreferences (SHARED_PREFS, MODE_PRIVATE);
        String language = sharedPreferences.getString(EXTRA_LANGUAGE, "0");
        Log.d("PhoneNumberActivity", "localeToSwitchTo: language is: "+ language);
        super.attachBaseContext(MyContextWrapper.wrap(newBase, language));
    }
0 Answers
Related