How to use NSLocalizedString function with variables in Swift?
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)
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)
I have followed the approach of creating extension to String as i have many strings to localize.
extension String { var localized: String { return NSLocalizedString(self, comment:"") }}
To use it for localization in code do:
self.descriptionView.text = "Description".localized
For strings with dynamic variables follow :
self.entryTimeLabel.text = "\("Doors-open-at".localized) \(event.eventStartTime)"
Declare the strings in String files for different languages (example : Arabic and English)
Hope will be helping!