Localized string with argument adds line breaks and brackets around argument

Viewed 1264

I am trying to display a localized string that contains an argument. Instead of displaying the string in one line with the argument embedded, the result is a broken 3-line string:

Expected result:

The price is $9.99/year.

Result:

The price is (
    "$9.99"
)/year.

Localizable.strings:

"price_string" = "The price is %@/year.";

Call:

"price_string".localized(priceString)

where priceString is a String variable.

And .localized() is defined as such:

extension String {
    var localized: String {
      return NSLocalizedString(self, comment: "\(self)_comment")
    }

    func localized(_ args: CVarArg...) -> String {
      return String(format: localized, args)
    }
}
1 Answers

Please look at the output. It shows clearly that the price argument is an array. And indeed the variadic parameter args is treated as an array.

So you are just using the wrong API

func localized(_ args: CVarArg...) -> String {
     return String(format: localized, arguments: args)
}
Related