valfer valfer - 10 months ago 141
Swift Question

iOS Swift and localizedStringWithFormat

I could not find a substitute for

in swift. What I am trying to do is to use the singular/plural localization using the
, english example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
<key>%d record trovati</key>
<string>%#@num_people_in_room@ in the room</string>
<string>No record</string>
<string>Only one record</string>
<string>Some records</string>

and the code:

[NSString localizedStringWithFormat:NSLocalizedString(@"%d record trovati", nil), totRecsFound];

Any idea on how to do this in Swift?

Answer Source

The same methods are available in Swift:

for totRecsFound in 0 ... 3 {
    let str = NSString.localizedStringWithFormat(NSLocalizedString("%d record trovati", comment: ""), totRecsFound)


No record in the room
Only one record in the room
Some records in the room
Some records in the room

Note that in addition to the "Localizable.stringsdict" file there needs to be a "Localizable.strings" file (which may be empty).