I've the following piece of code:
/**
* Sets a new Locale for the APP.
* @param newLocale - Valid new locale.
*/
private static void setLocale( String newLocale )
{
Locale locale = new Locale( newLocale );
Locale.setDefault( locale );
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration( config, context.getResources().getDisplayMetrics() );
}
Simple.
However when I run it in a Smartphone (4.1.1), it does work flawlessly. The device changes Strings in order to match the language.
But with a tablet (4.3), it doesn't work. If I output something like:
Log.d("TAG",Locale.getDefault());
The Locale seems to be changed on both devices, but as I said, Strings doesn't get translated to the correct language.
I've done a lot of debugging, and I've spotted a difference between objects: Check out the Configuration object on 4.1.1:
And check out the Configuration Object on the tablet (4.3)
As you can see, the only notable difference is the userSetLocale
which is set to False on tablet.
So I checked Google SourceCode (https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/res/Configuration.java), and it states:
/**
* Locale should persist on setting. This is hidden because it is really
* questionable whether this is the right way to expose the functionality.
* @hide
*/
public boolean userSetLocale;
Looks like this is affecting me. So as I can't access this value, nor by getter / setter nor by public access, I used reflection in order to change it.
However, after changing it by reflection, even though I've seen that it internally changed (boolean is set to false after reflection), same issue is still up.
Do you guys have any tips?
Meanwhile I will keep testing.
Thanks. TESTING:
Well,
User Sir SC provided an answer which worked. But my code, also worked. The issue we were facing, was about a single device ignoring this locale change. So AFAIK, it's just a single device, and it might be caused because of a buggy ROM, as it was a Genymotion Emulator.
So overall, the answer is:
Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With