Switch language programmatically on an Android device
Asked Answered
G

3

9

I'm writing some Android test automation for our applications, which are localized into 18 languages.

I need the ability to switch the language on the device programmatically - I don't mean to switch the language of just the application - I mean switch the actual device language in the Android settings, that way our application switches also.

Is there any way I can accomplish this via MonkeyRunner, the Android Debug Bridge or otherwise? Ideally I'd like a way that works on both the android emulator, and a physical device.

Thanks.

Gills answered 13/12, 2011 at 20:9 Comment(0)
G
4

Finally figured out you can do this via the Android Debug Bridge which I've just installed on my test machine and added to the windows PATH variable.

For example, the command to switch the device language to German would be:

adb shell "su -c 'setprop persist.sys.language de; setprop persist.sys.country de; stop; sleep 5; start'

You will need a rooted device in order for this to work.

Gills answered 5/1, 2012 at 18:27 Comment(0)
A
1

You can change the local like this :

   Resources res = context.getResources();
    // Change locale settings in the app.
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(language_code.toLowerCase());
    res.updateConfiguration(conf, dm);
Actomyosin answered 13/12, 2011 at 20:24 Comment(1)
Is this setting the locale from the mobile applications code? If so, I don't think you read my question correctly - I need to switch the device language, from an automation application independent from my mobile applications code.Gills
O
0

You can write a script, which sends a broadcast to a helper app like adb send broadcast and this app changes your configuration inside the device like device language where you can use that code of @iSun.

Organization answered 20/1, 2014 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.