Language and locale automated testing on mobile apps

Find out how to perform localisation testing with mobile apps. Change your language or locale with Appium, XCUITest and Espresso.

By Jochen D.

Localization testing allows you to test your native mobile app on iOS and Android devices with various language and localization settings.
It is important to make sure that your mobile app works correctly on devices with other languages and locales, both functionally and in terms of design.

Some languages might use longer sentences than English, which might break your UI.
For example, you have a sentence in your app that fits perfectly inside the UI for English, but the translation in Portuguese is too long, causing the sentence to go beyond the allowed size.
Another example is where your app displays a number, currency, or date in a different format due to other locale settings. Setting various locale settings during your tests will allow you to test all these scenarios.

In this article we'll focus on how to perform language and locale testing, with both Appium, XCUItest (iOS) and Espresso (Android).

What is the difference between language and locale?

The terms locale and language are often used in the same context, but they have different meanings.

The term language indicates the language on the device. It is the language used in the UI of the device's OS and can often be changed in the Settings menu of your OS.
Examples of a language are: English, French, Portugese, Dutch, Spanish, Chinese and more. iOS supports more than 30 languages, while Android supports a larger amount of languages.

Locale refers to regional settings, for a specific country or place. This includes settings such as currency format, number format, or date formats.
Countries can share the same language, but have different locale settings.

To get a list of available locales on Android, you can use this snippet of code:

Copy code
for (Locale locale : Locale.getAvailableLocales()) {
	Log.d("LOCALES", locale.getLanguage() + "_" + locale.getCountry() + " [" + locale.getDisplayName() + "]");
}

How to change the language in an Appium test?

Appium provides a language capability, which you can use to pass in the desired language code (ISO 639-1) of the app under test.

When you compile your IPA or APK file, you can indicate which languages you want to support and provide the translated content.
For Android, translated content usually appears in strings.xml files, while on iOS you can use XCode's localization features.

Copy code
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("language", "es");

To fetch a all translated strings for a specific language, you can use the app_strings command:

Copy code
let appStrings = driver.getStrings("<language_code>");

How to change the locale in an Appium test?

To change the locale, you can use Appium's locale capability, which accepts region codes.

By changing the locale, you can test regional settings, such as currency formats, date formats, time and calendar formats.
All these settings are country/region specific and can be tested with Appium's locale capability.

Copy code
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("locale", "fr_CA");

Changing the language or locale will change the settings on a device level for Android, and on an app-level for iOS.

How to set a different language or locale with XCUITest?

XCUITest offers two arguments which you can use to set the language or locale:

  • -AppleLanguages
  • -AppleLocale

To use this in XCUITest, you can pass in a language or locale of your choosing like this:

Copy code
XCUIApplication().launchArguments += [-AppleLanguages, (fr)]
XCUIApplication().launchArguments += [-AppleLocale, fr_FR]
XCUIApplication().launch()

In the example above, we set the language to French and the locale to France/French.
XCUITest will open the app with these settings. If your app has localized strings for this language, your app should display in the correct language.

Apple provides some more information about setting up and testing localized apps in their Testing your International App documentation.

How to change the language or locale with Espresso?

During an Espresso test, you can programmaticaly switch the language or locale of the app.
Please see the example below on how to achieve this:

Copy code
private void setLocale(String language, String country) {
    Locale locale = new Locale(language, country);
    // here we update locale for date formatters

    Locale.setDefault(locale);
    // here we update locale for app resources

    Resources res = getContext().getResources();
    Configuration config = res.getConfiguration();
    config.locale = locale;
    res.updateConfiguration(config, res.getDisplayMetrics());
}

This function accepts a language and country, it will update the locale and resources during the Espresso test.

Ready to start testing?

Start a free trial