Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSLocalizedString function with variables in Swift?

I'm trying to localize my app using NSLocalizedString. When I import the XLIFF file, most works like a charm but something do not and some string is not localized. I have noticed that the problem is from NSLocalizedString containing something variable inside like:

NSLocalizedString(" - \(count) Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare") 

or

NSLocalizedString("Notifica per \(medicina!) della prescrizione \(prescription!)\nMemo: \(memoTextView.text)", comment: "Messaggio della Local Notification") 

Maybe this is not the correct syntax for this kind of stuff. Someone can explain me how to do that in Swift?

like image 326
Andorath Avatar asked Oct 09 '14 11:10

Andorath


2 Answers

You can use the sprintf format parameters within NSLocalizedString, so your example can look like this:

let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count) 
like image 56
zisoft Avatar answered Sep 20 '22 13:09

zisoft


In Session #412 of the WWDC2014 "Localizing with Xcode 6" the proper way to this in Swift is the following:

String.localizedStringWithFormat(     NSLocalizedString(" - %d Notifica",     comment: "sottotitolo prescrizione per le notifiche al singolare"),     count) 
like image 31
Mark Avatar answered Sep 18 '22 13:09

Mark