带参数的本地化字符串在参数周围添加换行符和括号

Kqt*_*qtr 4 nslocalizedstring ios swift

我正在尝试显示包含参数的本地化字符串。结果不是在一行中显示嵌入参数的字符串,而是一个断开的 3 行字符串:

预期结果:

The price is $9.99/year.
Run Code Online (Sandbox Code Playgroud)

结果:

The price is (
    "$9.99"
)/year.
Run Code Online (Sandbox Code Playgroud)

Localizable.strings:

"price_string" = "The price is %@/year.";
Run Code Online (Sandbox Code Playgroud)

称呼:

"price_string".localized(priceString)
Run Code Online (Sandbox Code Playgroud)

其中priceString是一个字符串变量。

.localized()定义如下:

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

    func localized(_ args: CVarArg...) -> String {
      return String(format: localized, args)
    }
}
Run Code Online (Sandbox Code Playgroud)

vad*_*ian 5

请看输出。它清楚地表明price参数是一个数组。事实上,可变参数args被视为一个数组。

所以你只是使用了错误的 API

func localized(_ args: CVarArg...) -> String {
     return String(format: localized, arguments: args)
}
Run Code Online (Sandbox Code Playgroud)