Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift: Getting localized string with an argument from String Catalog

I have the following key and its translation in String Catalog (Xcode 15.3):

enter image description here

when I put the key in a SwiftUI‘s Text like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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 without arguments is sufficient.

String(format: NSLocalizedString("login success subTitle%@", comment: ""), appName)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading