What’s the equivalent to String.localizedStringWithFormat(_:_:)
in SwiftUI?
I know LocalizedStringKey.init(:)
can make use of string interpolation, but as I understand it this requires localizable string keys to be parameterized in the .strings/.stringsdict files.
This is different to how localized string keys are currently defined in the app I'm working on. Given these localizable strings in Localizable.strings:
"HELLO_WORLD" = "Hello, world!";
"HELLO_WORLD_PARAMETERIZED" = "Hello, %@!";
this works just fine in the Foundation/UIKit world:
NSLocalizedString("HELLO_WORLD", comment: "") // "Hello, world!"
String.localizedStringWithFormat(NSLocalizedString("HELLO_WORLD_PARAMETERIZED", comment: ""), "Bob") // "Hello, Bob!"
But I don’t see how to make it work in SwiftUI:
let helloWorld = LocalizedStringKey("HELLO_WORLD")
Text(helloWorld) // ✅ Displays "Hello, world!"
Text("HELLO_WORLD") // ✅ Also displays "Hello, world!"
LocalizedStringKey("HELLO_WORLD_PARAMETERIZED", "Bob") // ❌ This won't compile, because LocalizedStringKey has no format parameter like `String.localizedStringWithFormat(_:_:)` does.
let bob = "Bob"
LocalizedStringKey("Hello, \(bob)!") // 🤔 This *would* work if the key in Localizable.strings were "Hello, %@!" – but this doesn't reflect the reality of localized string keys are currently defined for this app.
Text(verbatim: String.localizedStringWithFormat(NSLocalizedString("HELLO_WORLD_PARAMETERIZED", comment: ""), "Bob")) // 🙈 This correctly displays "Hello, Bob!" in the Text view, but... well, it ain't pretty.
Do I have to change all my localizable string keys (as opposed to just their values) to be parameterized in order to be able to use them in SwiftUI views (without using the Text(verbatim:)
workaround)?