1

EDIT:

I may be wrong in my understanding, but this is a different question than Set Locale programmatically since I already implemented the answer suggested there and I still have some issues.

In this questions I am asking for help in resolving that issues (issues that have no reference in the Set Locale programatically question).

Original post

I am trying to implement custom locale in my application and run into several issues.

I am using the below code in all my activities just before the call to setContentView:

Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = new Locale(newLocale);;
res.updateConfiguration(conf, dm);

EDIT:

I also tested it with:

Resources res = getBaseContext().getResources();

I added to all activities in AndroidManifest.xml the below attribute:

android:configChanges="locale|orientation"

When the user select a new Locale I also run the above code to update the configuration.

The issues that I run into are:

  1. After changing the Locale only updated text in the current activity uses the new Locale (also when I press "back" in the previous activity only refreshed text uses the new Locale (If I start a new activity it uses the new Locale as expected).
  2. I use DataUtils to format dates, but it seems that whatever I do it ignores the new Locale.
  3. Same issue with DataPicker, I could not get it to use the new Locale.
  4. I could not make the new Locale effect my app widget.

For the first issue I solved it by applying the change only when the application start, and when the user update the Locale I show a message requesting the user to restart the application for the change to take effect. I prefer to make the change without restarting the application, but could not figure out how to make it work properly.

The second issue can be resolved if I use SimpleDateFormat with the custom Locale, however I prefer to use DateUtils if possible.

For the last two issues I could not find any way to overcome it.

Any help is appreciated.

user905686
  • 4,491
  • 8
  • 39
  • 60
Hanan
  • 459
  • 1
  • 5
  • 16
  • Unfortunately it is not, I tried it before posting this question. – Hanan Aug 11 '14 at 20:00
  • Please edit the question to explain why it is not a duplicate of the one suggested. – AdrianHHH Aug 11 '14 at 21:09
  • Edits upon on top of edits makes it hard to read and parse this question. Can you perform another edit and consolidate the material into one cohesive question? Also, even after your edits, its not clear where you implemented Igor's answer from the duplicate. You should include that code, too. – jww Aug 18 '14 at 03:21
  • It is a subtly different question, not an exact dupe. It shouldn't be closed. – Gabe Sechan Aug 18 '14 at 04:03
  • Take a look at my [new answer](https://stackoverflow.com/a/47223492/905686) to "Set Locale programmatically". Things have changed meanwhile and this should work. – user905686 Nov 20 '17 at 16:28

1 Answers1

3

THe reason you're not seeing the changes is that Android really wasn't created for this use case. Locales are expected to be changed via the system setting, not on a per app basis. Setting the Locale via configuration only changes it for this Activity, it does not set it globally for the app. If you want to do it globally, you need to store the current locale yourself and manually set it in onResume of each activity in your app.

I'm not sure if there's any way to make builtin utilities respect a changed locale, as they're probably looking at the system locale. You may need to take the widget from the AOSP, copy the code, and alter it to look at your locale variable rather than use the built in versions.

Its a lot of work. I don't know what your app is doing, but I would reconsider if this functionality is truly needed.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127