I created a Localizable.strings file and the translation works fine. But there is a special case where in english there is one word for singular and plural, like \'series\'.
This is exactly what the Plural Rule Properties in Stringsdict files are for.
So in addition to the "Localizable.strings" file you have to provide a "Localizable.stringsdict" property list file with plural rules for the language. In your case:
Localizable.strings:
"%ld series" = "%ld Serien";
Localizable.stringsdict:
<plist version="1.0">
<dict>
<key>%ld series</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@series@</string>
<key>series</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>ld</string>
<key>one</key>
<string>%ld Serie</string>
<key>other</key>
<string>%ld Serien</string>
</dict>
</dict>
</dict>
</plist>
Note that the proper format specifier for Int
(which can
be a 32-bit or 64-bit integer) is %ld
.
Now everything works "automatically", with no changes in the Swift code:
for n in 1...3 {
let str = String(format: NSLocalizedString("%ld series", comment: ""), n)
print(str)
}
Output:
1 Serie 2 Serien 3 Serien
Even if more languages with other plural rules are added, no changes in the Swift code are necessary.
How I do it is do my localisation names as follows: series = "series" and "Serie" and series_pural = "series" and "Serien". Then the system just looks for a _plural afterwards and if it exists then it'll show it (if you tell it that its a plural word that u are localising). Another way would just you handle whether or not a word is a plural on a case by case basis.
There are quite a lot of ways how is possible to do this. But i prefer to use way that is introduced by Apple it s quite easy and clear.
Use NSLocalizedString
with checking of the amount in you're case.
Example :
let toast: String
if days == 1 {
toast = NSLocalizedString("Serie.one", comment: "")
}
else {
toast = String(format: NSLocalizedString("Serien.other", comment: ""), days)
}
Localizable.strings contains this information:
Serie.one = "Serie";
Serien.other = "Serien %d ";