Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing locale programmatically not working in some devices

Tags:

java

android

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:

enter image description here

And check out the Configuration Object on the tablet (4.3) enter image description here

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:

  • Nexus 10 - 4.4.2 - OK
  • Nexus 5 - 4.4.2 - OK
  • Tablet 320 dpi - 4.4.2 - OK
  • SmartPhone 480 dpi - 4.3 - OK
  • SmartPhone 160 dpi - 4.1.1 - OK
  • Tablet 160 dpi - 4.3 - NOT OK
  • SmartPhone 320 dpi - 4.1.1 - OK
like image 730
Reinherd Avatar asked Apr 17 '14 11:04

Reinherd


Video Answer


1 Answers

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:

  • The code OP posted, is valid and working. So is the code from Sir SC.

Cheers.

like image 69
Reinherd Avatar answered Oct 01 '22 18:10

Reinherd