I have a function to get the localized string for plural cases.
extension String
static func localizedStringForPlurals(
formatKey key: String,
comment: String = "",
args: CVarArg... ) -> String
{
let format = NSLocalizedString(key, comment: comment)
var result = withVaList(args){
(NSString(format: format,
locale: NSLocale.current,
arguments: $0) as String)
}
return result
}
}
The key point is NSLocalizedString to get localized string for plural case from dict file.
I have an example of getting localized string for taps. If the int number is one, the localized string is "This tap"; if int is larger than one, i.e., others, the string is "These taps". See attachment dict file (English).
It works fine for English. For example:
var count: Int
...
let locS = String.localizedStringForPlurals(
formatKey: "theseTaps", args: count) // "This tap" if count is 1 // "These taps" if count is > 1
However, it does not work well for Chinese. Here is the similar dict file for Chinese.
The result in Swift code is always plural string, i.e., "These tap", even count is 1.
I am not sure if this is a bug in NSLocalizedString or not. I know that in Chinese in general there are no plural cases. However, as in this example, there are plural cases, this tap or these tap. In Chinese there is no plural for "tap", but "this", and "these" in Chinese are different, and they are plural cases.
Any way to resolve the issue? I do like Apple's localization for plural framework. I would like this framework works as developer's expectation, as in my example in two dict files.