Reputation: 479
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:
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.
Upvotes: 1
Views: 2872
Reputation: 93542
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.
Upvotes: 3