Hons
Hons

Reputation: 4046

XCTest under different locale

Is it possible to test the outcome of a test under a different device locale?

I'd like to test method that is using

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

To determine the current device language. Now in my XCTest I'd like to check what happens when I'm in e.g."en-gb" instead of just "en", or simply test for a completely different language.

Upvotes: 6

Views: 1550

Answers (1)

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

The NSLocalizedString methods load the appropriate strings for the keys on launch for fast lookup, so you can't shift it around after launching. What you can do is run a test suite with one locale, then run the same test suite in a different locale.

You can set the locale for an app run in the Scheme Editor by selecting the Run action, Options tab, and picking Application Language and Application Region. Both default to the System settings, but you can change them, including using some convenient fake RTL and fake double-length languages.

(If the Test action doesn't inherit those settings for whatever reason, then there are also commandline arguments you can set to alter the defaults lookup used to order language and region preferences. This might be desirable for scripting purposes anyway.)

Upvotes: 3

Related Questions