Launch argument "-AppleLocale en_US" doesn't change measurement system(units) to "US" on MacOs

We used Locale::usesMetricSystem to distinguish between metric and Imperial(US) measurement systems. We set "-AppleLocale en_US" as launch argument for App in UITests but usesMetricSystem still returns true in case user's locale are not equal to "US" in System Preferences -> Language and Region -> Region.

Here is a demo project to illustrate the issue: https://github.com/yuri-qualtie/UseMetricSystemDemo

Precondition: Change user region to not US(for example Australia) in System Preferences -> Language and Region -> Region

We expect that code:
Code Block
print(Locale.current.usesMetricSystem)

prints false when -AppleLocale en_US is set but actual result is true

Probably there is alternative way how to change measurement system via launch arguments or App's settings in Xcode?
Answered by DTS Engineer in 671734022
Try this [1]:

Code Block
-AppleLocale en_US -AppleMetricUnits <false/> -AppleMeasurementUnits Inches


There are two things to note here:
  • These preferences keys are not considered API. It’s fine to use them for tests and so on, but you should not ship code that relies on them. Rather, continue to use Locale APIs like usesMetricSystem.

  • Apropos usesMetricSystem, be aware that we no longer use a strict Boolean value here. The popup now shows three values (Metric, US, UK) reflected in NSLocaleMeasurementSystem. I believe NSMeasurementFormatter will do the right thing here, but it’s not something I’ve dug into in detail.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Weirdly, if you’re testing this in a command-line tool (rather than app), you have to initialise the UserDefaults system before you get the locale. So this code:

Code Block
_ = UserDefaults.standard
let loc = Locale.current
print(loc)


prints en_US but, if you drop the first line, it prints your current locale (en_GB in my case).

Did anyone on this thread file a bug requesting that Xcode add direct support for this? If so, please post the bug number.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Launch argument "-AppleLocale en_US" doesn't change measurement system(units) to "US" on MacOs
 
 
Q