Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch language programmatically on an Android device

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.

like image 798
Jimmy Collins Avatar asked Dec 13 '11 20:12

Jimmy Collins


2 Answers

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.

like image 79
Jimmy Collins Avatar answered Sep 18 '22 23:09

Jimmy Collins


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);
like image 44
iSun Avatar answered Sep 18 '22 23:09

iSun