I have the following key and its translation in String Catalog (Xcode 15.3):
when I put the key in a SwiftUI
‘s Text
like this:
let appName = "some name"
Text("login success subTitle(appName)")
it works well and returns the English translation with the argument (some name).
But what I’m trying to do is to return the English translation using a helper function to use it somewhere else other than SwiftUI
‘s Text
, so I’ve tried the following:
String(format: "login success subTitle%@", arguments: [appName])
but it keeps returning the key (without the argument) instead of the translation (with the argument).
Any thoughts are appreciated.
2
Answers
The
format
parameter contains just a literal string, unlike in SwiftUI there is no implicit connection to the localized string key.You have to call
NSLocalizedString
and the simple API withoutarguments
is sufficient.The string interpolation mechanism from SwiftUI’s
Text
view works withString(localized:)
as well: