arcgis-maps-sdk-swift-samples
arcgis-maps-sdk-swift-samples copied to clipboard
String interpolation in SwiftUI Text doesn't work with system locale correctly
Text("Z-value: \(model.zValue.formatted(.measurement(width: .narrow).locale(Locale(identifier: "en-CA"))))")
Shows as "Z-value: 70m"
Text("Z-value: \(model.zValue, format: .measurement(width: .narrow).locale(Locale(identifier: "en-CA")))")
Shows as "Z-value: 230'"
I suspect it is because SwiftUI views get the locale from the environment. Does this show the expected value?
Text("Z-value: \(model.zValue, format: .measurement(width: .narrow))")
.environment(\.locale, Locale(identifier: "en-CA"))
Does this show the expected value?
Yes it shows the correct unit as "Z-value: 70m". It kind of makes sense that the priority is measurement string formatting locale > environment locale > measurement value string interpolation format style locale, but still hard to remember.

I set the locale in Simulator as this - locale ID is still "en-US" while the measurement system is changed to Metric.
Using the older formatter API, the dimensions are correctly displayed as metric units, while the SwiftUI formatting style, or formatted() are displayed as imperial.
I'll keep digging to see if SwiftUI formatting style APIs support displaying a different measurement system than the locale's default.
I'll keep digging to see if SwiftUI formatting style APIs support displaying a different measurement system than the locale's default.
MeasurementFormatUnitUsage.asProvided might be what you're looking for.
On iOS 16 and newer there is Locale.measurementSystem, which sounds like what you want. It can be specified by creating a locale using Locale.init(components:):
var components = Locale.Components(locale: .current)
components.measurementSystem = .metric
let locale = Locale(components: components)
Thanks Phil. This definitely looks like the correct way to go. Though I tested in iOS 16, it doesn't behave as expected. I'll add this issue to iOS 16 and beyond column and take a look in the future.