I'm trying to localize string which contains range like 1..2. I'm using String.localizedStringWithFormat
:
func testLocalizableString() -> String {
let lowerBound = 1
let upperBound = 2
return String.localizedStringWithFormat(
NSLocalizedString("Unit.Meters.Range", value:"%d-%dm", comment: ""),
[lowerBound, upperBound]
)
}
However, I get strange result: "103,413,600-0m".
If I use only one argument (just "%dm", not "%d-%dm") and pass only one number everything is fine.
What could be wrong with my code and how to properly format localizable string with CVarArg argument?
The second parameter in
String.localizedStringWithFormat(_ format: String, _ arguments: CVarArg...)
is a variadic parameter, which means that you must pass zero or more arguments of the specified type, not an array:
func testLocalizableString() -> String {
let lowerBound = 1
let upperBound = 2
return String.localizedStringWithFormat(
NSLocalizedString("Unit.Meters.Range", value:"%ld-%ldm", comment: ""),
lowerBound, upperBound
)
}
Note also that the format specifier for Int
is %ld
,not %d
.