15

I have an application that has support for the Portuguese language, both for Portugal and Brazil regions. I have created a values-pt (containing the Portugal translation) and values-pt-rBR (containing the Brazilian translation). I then tried changing the application language to Potuguese (Portugal) and the application language changes correctly. When I set it to Portuguese (Brazilian) it does not. I tried changing the phone default language to Portuguese (Brazilian) and it still does not work. It works for Portuguese (Portugal) in both cases (programmatically and system). Does anyone know what the problem is? The code for programmatically changing the application's locale is the following:

Locale locale = new Locale(strLocale);
Locale.setDefault(locale);

config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

and strLocale is set to "pt" or "pt_BR" for Portugal and Brazil language respectively.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
a.p.
  • 3,248
  • 7
  • 30
  • 48
  • Another simple way is to use Locale.forLanguageTag("pt-BR"). For more details https://docs.oracle.com/javase/tutorial/i18n/locale/matching.html – Swapnil Kadam Sep 20 '22 at 21:22

2 Answers2

39

I assume you're targeting Android OS > 2.2, because both Portuguese languages are supported by Android starting at version 2.3+.
I tested it on the emulator, by swapping language & keyboard and having only 1 string different: app_name, so it appeared in application title and it worked just fine.

The only difference is that I named Portugal region values-pt-rPT insead of values-pt.
I didn't test on the fly-relocalization, but I guess it works as well.
I think that when you work with regions, you MUST specify a region for every localization (i.e. fr-rFR for France and fr-rCA for Canada) and can't use the default (pt in your case, or fr in my second example).

This link illustrates the whole thing more deeply.

[EDIT]

When creating a new locale by code, try:

final Locale myLocale = new Locale("pt", "PT");

and

final Locale myLocale = new Locale("pt", "BR");

Here's another page from the reference site.

Community
  • 1
  • 1
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    I have tried what you suggested but it looks like the problem is that my phone reports the currently selected locale as 'pt', no matter if I choose Portuguese (Portugal) or Portuguese (Brazil). Is there anything wrong with my phone? – a.p. Dec 12 '13 at 15:07
  • No. Is it Android 2.3+? How do you setup your locale (it can be done in different ways - for my needs, Canadian French is the same as French French and Austrian German is the same as German German - I just use the DEFAULT language - in your case, pt). – Phantômaxx Dec 12 '13 at 15:11
  • My phone is running Android 4.1.2. It gives me pt for both Portugal and Brazil. Nevertheless, my biggest problem is that the statement locale = new Locale("pt", "PT") does not seem to work either. I set from cod the locale to pt "PT", I have folders values-pt-rPT and values-pt-rBR and the application does not change. If I have a values-pt folder and change the locale to new Locale("pt"), it works. Is there something else I am missing? – a.p. Dec 12 '13 at 15:47
  • One correction to the above, using Locale.getDefault().toString() returns the proper locale string, i.e., pt_PT or pt_BR. The problem that remains is how to set the locale programmatically. Any other ideas? – a.p. Dec 12 '13 at 16:06
  • 1
    After further testing, setting the locale as you suggested, i.e., new Locale("pt", "BR") or PT for Portugal solves the problem. Thanks for the help! – a.p. Dec 12 '13 at 16:25
  • My pleasure, I'm here to help – Phantômaxx Dec 12 '13 at 16:28
  • @user501223 Guys, I'm having troubles with Netherlands. I use: final String countryCode = curSelectedStore.getCountryCode(); final Locale locale = new Locale(language.getCode(), countryCode); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; final Resources resources = context.getResources(); final DisplayMetrics displayMetrics = resources.getDisplayMetrics(); resources.updateConfiguration(config, displayMetrics); And I got my folders "values-nl-rNL" and "values-nl-rBE" and I couldn't make it work. Thanks! – cesards Jul 25 '14 at 09:07
  • Please post a new question, with a mention to this one. – Phantômaxx Jul 25 '14 at 11:14
0
Locale.setDefault(new Locale("pt", "BR"));
j__carlson
  • 1,346
  • 3
  • 12
  • 20
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 10 '21 at 05:57
  • This is effectively what [the other answer](https://stackoverflow.com/a/20528804/1364007) says. – Wai Ha Lee Oct 11 '21 at 08:23