I'm developing app that supports "en" and "ru" languages, users can select language inside app.
If default phone's locale set to "en", but inside app selected "ru" language, then when trying to localize plural sentence ignored 'many'/'few' form. So it's localized by the English plural rules.
Definition:
<key>%d files</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@files@</string>
<key>files</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>one</key>
<string>один файл</string>
<key>many</key>
<string>%d файлов</string>
<key>other</key>
<string>%d файла</string>
</dict>
</dict>
Code to localize (manually create 'ru' locale):
let locale = NSLocale(localeIdentifier: "ru_RU")
String(format: NSLocalizedString("%d files", comment: ""),
locale: locale,
count)
As output i got:
for count = 1: "один файл" - and it's right
for count = 2: "2 файла" - it's also right (from category 'other')
for count = 6: "6 файла" - wrong, also from category 'other', but should be taken from 'many'
If i switch phone's language to Russian, then all being localized correctly.
You should use keys:
one for 1 = один файл
few for 2-4 = 2 файла
other for all other cases
It is quite old question, but I've faced the same problem and this is how I fixed this:
let path = Bundle.main.path(forResource: "ru", ofType: "lproj")!
let bundle = Bundle(path: path)!
let localizedString = String(format: NSLocalizedString("%d files", bundle: bundle, comment: ""),
locale: NSLocale(localeIdentifier: "ru_RU"),
count)
The main idea is to make NSLocalizedString
to search in language-specific bundle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With