Skip to content

Support deferred formatting #83

@liamnichols

Description

@liamnichols

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:

https://developer.apple.com/documentation/swift/string/localizationvalue/stringinterpolation/appendinterpolation(_:format:)-5arer

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions