How to use NSLocalizedString function with variables in Swift?

IosSwiftLocalizationNslocalizedstringXliff

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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)

Solution 2 - Ios

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)

Solution 3 - Ios

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)

enter image description here enter image description here

Hope will be helping!

Solution 4 - Ios

I tried the above solutions however the code below worked for me

SWIFT 4

extension String {

    /// Fetches a localized String
    ///
    /// - Returns: return value(String) for key
    public func localized() -> String {
        let path = Bundle.main.path(forResource: "en", ofType: "lproj")
        let bundle = Bundle(path: path!)
        return (bundle?.localizedString(forKey: self, value: nil, table: nil))!
    }


    /// Fetches a localised String Arguments
    ///
    /// - Parameter arguments: parameters to be added in a string
    /// - Returns: localized string
    public func localized(with arguments: [CVarArg]) -> String {
        return String(format: self.localized(), locale: nil, arguments: arguments)
    }

}

// variable in a class
 let tcAndPPMessage = "By_signing_up_or_logging_in,_you_agree_to_our"
                                     .localized(with: [tAndc, pp, signin])

// Localization File String
"By_signing_up_or_logging_in,_you_agree_to_our" = "By signing up or logging in, you agree to our \"%@\" and \"%@\" \nAlready have an Account? \"%@\"";

Solution 5 - Ios

Here is an extension I use in String, it adds a localizeWithFormat function with variable arguments,

extension String {

     func localizeWithFormat(arguments: CVarArg...) -> String{
        return String(format: self.localized, arguments: arguments)        
     }
            
     var localized: String{
         return Bundle.main.localizedString(forKey: self, value: nil, table: "StandardLocalizations")
     }
}

Usage:

let siriCalendarText = "AnyCalendar"
let localizedText = "LTo use Siri with my app, please set %@ as the default list on your device reminders settings".localizeWithFormat(arguments: siriCalendarTitle)

Just be careful not to use the same function and property names that String has. I normally use a 3 letter prefix for all my framework functions.

Solution 6 - Ios

I wrote the same functions for UILabel

extension UILabel {
    
    func setLocalizedText(key: String) {
        self.text = key.localized
    }
    
    func setLocalizedText(key: String, arguments: [CVarArg]) {
        self.text = String(format: key.localized, arguments: arguments)
    }
}

If you want you can move this localized property to UILabel as well

extension String {
        
    var localized: String{
        return Bundle.main.localizedString(forKey: self, value: nil, table: nil)
    }
}

My localizable

"hi_n" = "Hi, %@!";

Used them like this:

self.greetingLabel.setLocalizedText(key: "hi_n", arguments: [self.viewModel.account!.firstName])
// Shows this on the screen -> Hi, StackOverflow!

Solution 7 - Ios

I created an extension to String since I had many strings to be localized.

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

For example:

let myValue = 10
let anotherValue = "another value"

let localizedStr = "This string is localized: \(myValue) \(anotherValue)".localized
print(localizedStr)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndorathView Question on Stackoverflow
Solution 1 - IoszisoftView Answer on Stackoverflow
Solution 2 - IosMarkView Answer on Stackoverflow
Solution 3 - IosJaspreetKourView Answer on Stackoverflow
Solution 4 - IosPratikView Answer on Stackoverflow
Solution 5 - Iosthe ReverendView Answer on Stackoverflow
Solution 6 - IosOhhhThatVarunView Answer on Stackoverflow
Solution 7 - IosSantoshView Answer on Stackoverflow