-
-
Notifications
You must be signed in to change notification settings - Fork 49
Description
If we take the following example:
let date = Date()
var resource: LocalizedStringResource = .localizable.todaysDate(date.formatted(.dateTime))
resource.locale = Locale(identifier: "en_US")You might at a glance expect the date to be formatted using en_US formatting rules, but it is not. This is because date.formatted(date: .numeric, time: .omitted) is resolving a String using Locale.current prior to passing it into the LocalizedStringResource.
Without using XCStrings Tool, if we wanted to format this using the resource (or SwiftUI environment) locale, we'd have to do the following:
var resource: LocalizedStringResource = "Today's date is \(date, format: .dateTime)"
resource.locale = Locale(identifier: "en_US")\(date, format: .dateTime) is subtly different to date.formatted(.dateTime) because it uses a custom interpolation method:
It will hold the original value inside the resource alongside the FromatStyle and will only resolve it when required, using the locale information provided.
Because XCStrings Tool only generates methods with a String argument, we cannot benefit from this deferred formatting, which is unfortunate. It would be nice if we could.