3

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

5
  • did you tried to get resources after locale change? Commented Feb 23, 2016 at 11:45
  • Yes, I have tried to get strings after changing locale in android app but It returns the English language's strings. Commented Feb 23, 2016 at 12:42
  • does this work for another language (without region)? Also try to set location to getResources().getConfiguration() instead of creation new one. Commented Feb 23, 2016 at 13:06
  • Yes, it works for other languages. Ok, I will try the suggested solution. Commented Feb 23, 2016 at 14:05
  • I have tried using getResources().getConfiguration() solution but it is also not working. Please post solution if anybody has these things working. Commented Feb 25, 2016 at 5:00

1 Answer 1

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

what is the SalonyFormatter class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.