Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support deferred formatting #83

Open
liamnichols opened this issue May 24, 2024 · 0 comments
Open

Support deferred formatting #83

liamnichols opened this issue May 24, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@liamnichols
Copy link
Owner

liamnichols commented May 24, 2024

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.

@liamnichols liamnichols added the enhancement New feature or request label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant