10

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
6
  • Okay. I'm done with testing. I did that with Genymotion emulators and two real devices. As you can see, the one with the issue seems to be the tablet with 160dpi and 4.3. IDK if this is a bug of that version. I'll try to keep testing devices.
    – Reinherd
    Apr 17, 2014 at 11:32
  • This is a problem when not all the languages are included in the compilation. That's why there were apps in the past like locale to work around this issue. The locale will be set, but most of the text will be left untranslated. There is no generic solution to this problem, only workarounds. The only thing you could do is compile Android yourself for that device with the appropiate languages attached. You can check which languages are installed and usable in the Android Settings menu. Apr 17, 2014 at 12:12
  • What? I guess I didn't explain myself good enough. I've several folders for every language. values-pl, values-zh, values-es, values-en, etc. The thing is that setting the locale with the method I've shown you, works for almost every device, so text uses correct values folder, but for some devices, it's ignored.
    – Reinherd
    Apr 17, 2014 at 14:35
  • Maybe I didn't understand the complexity of the question. But you mean in the scope of your application? Than everything should change if you have all the translations available (like you say you do). On a system level there could be problems with custom locale's. Apr 17, 2014 at 14:47
  • Yep. Translations are okay as other devices uses them flawlessly. Thats the weird thing. I've checked available locales with Locale.getAvailableLocales() and there were several locales that matched my new locale, for instance es_ES. Next week I will try to figure out if this just happens in a single device, which is Genymotion Emulator, or if it does happen as well on real devices. Meanwhile if you have some ideas, them are so welcome =)
    – Reinherd
    Apr 17, 2014 at 16:06

2 Answers 2

5
+25

This is the code I am using for localisation. It is used by + 1m people and never heard of any complaint about not changing strings so I hope it will work for you as well:

On top of class:

Locale myLocale;

Function:

    public void setLocale(String lang) { 
        myLocale = new Locale(lang); 
        Resources res = getResources(); 
        DisplayMetrics dm = res.getDisplayMetrics(); 
        Configuration conf = res.getConfiguration(); 
        conf.locale = myLocale; 
        res.updateConfiguration(conf, dm);
        reloadUI(); // you may not need this, in my activity ui must be refreshed immediately so it has a function like this.
    }

Function Call:

//example for setting English
setLocale("en_gb");
4
  • Method is somehow exactly the same :) theres no diff between creating a brand new config or updating current one. As I said, this issue just happens in a single device. I guess that ROM is not correct at all
    – Reinherd
    Apr 24, 2014 at 16:41
  • Yes I am aware they seem to be similar but not "exactly" the same. You create new config, I get it from resources. Trying this function won't hurt it is easy give it a go ^^ Like I said, for my project, I didn't get any complaint about Locale. Wanted to share with you
    – canova
    Apr 24, 2014 at 17:40
  • Hello. I've just tried. Same happens, I've tried that, from creating a new Locale, to getting current one and changing it. Same for Configuration. Check the emulator which doesn't want to work properly: i.imgur.com/TQtm5iq.png maybe you could try your APP in this device exactly (it's a Genymotion emulator)
    – Reinherd
    Apr 25, 2014 at 6:35
  • Creating a new Configuration and updating the current Configuration is NOT the same. If you create a new one you'll loose the old values. E.g. the large text accessibility settings will not be applied to your app (menus and settings font sizes will not reflect that setting which is useful for users with vision problems). Apr 25, 2014 at 16:54
1

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.

1
  • This is not how you answer. If a person has posted the valid code, mark that as answer and write your comments below it.
    – Irshu
    Jul 24, 2020 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.