Prithniraj Nicyone
Prithniraj Nicyone

Reputation: 5111

Change language programatically in Android with country code

I want to change my app locale to pt_BR. I have done the below things.

The code to change locale:

Locale locale;
if (localeName.contains("_")) {
           String localNameArray[] = localeName.split("_");
           locale = new Locale(localNameArray[0], localNameArray[1]);
} else {
       locale = new Locale(localeName);
}

Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);

string localeName contains pt_BR.

Created a new folder with name values-pt-rBR and added the strings.xml file in this folder.

But when I change my app's language to Portuguese Brazil (pt_BR), changes does not get reflected.

I have already checked the below links but not able to find any solution here:

Change language programatically in Android with country

Setting application locale to pt_BR programmatically

Upvotes: 3

Views: 3102

Answers (1)

orium
orium

Reputation: 3813

String files: values/string.xml and values-pt-rBr/string.xml

        setLocale(new Locale("en"));
        String eng = getString(R.string.hello_world);
        setLocale(new Locale("pt", "Br"));
        String bra = getString(R.string.hello_world);
        if (!eng.equals(bra)) {
            Log.i("locale_test", "it works!");
        }


public void setLocale(final Locale locale) {
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Locale.setDefault(locale);
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
}

update: starting from android N new approach is required
create ContextWrapper class and use it in your activity's attachBaseContext(Context) method

public class ContextWrapper extends android.content.ContextWrapper {

 public ContextWrapper(final Context base) {
    super(base);
 }

 public static ContextWrapper wrap(Context context, Locale newLocale) {

    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);

        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
    } else {
        configuration.setLocale(newLocale);
        DisplayMetrics dm = res.getDisplayMetrics();
        res.updateConfiguration(configuration, dm);
    }
    configuration.setLayoutDirection(newLocale);
    context = context.createConfigurationContext(configuration);

    return new ContextWrapper(context);
 }

}

@Override protected void attachBaseContext(final Context newBase) {
    String savedLocale = LocaleUtils.getSavedLocale();
    if (isNotEmpty(savedLocale)) {
        Locale appLocale = new Locale(savedLocale);
        ContextWrapper wrapped = ContextWrapper.wrap(newBase, appLocale);
        super.attachBaseContext(wrapped);
    } else {
        super.attachBaseContext(newBase);
    }
}

Upvotes: 3

Related Questions