What’s NSLocalizedString equivalent in Swift?

I use next solution:

1) create extension:

extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
    }
}

2) in Localizable.strings file:

"Hi" = "Привет";

3) example of use:

myLabel.text = "Hi".localized

enjoy! 😉

–upd:–

for case with comments you can use this solution:

1) Extension:

extension String {
    func localized(withComment:String) -> String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: withComment)
    }
}

2) in .strings file:

/* with !!! */
"Hi" = "Привет!!!";

3) using:

myLabel.text = "Hi".localized(withComment: "with !!!")

Leave a Comment