I realised I can use Morphology to specify the GrammaticalNumber based on the count value like this
private var formattedAttributedString: AttributedString {
let formattedCount: String = numberFormatter.string(from: count as NSNumber)!
var string: AttributedString = AttributedString(localized: "key_with_string_\(formattedCount)")
var morphology = Morphology()
let number: Morphology.GrammaticalNumber
switch count {
case 0: number = .zero
case 1: number = .singular
case 2: number = .pluralTwo
default: number = .plural
}
morphology.number = number
string.inflect = InflectionRule(morphology: morphology)
let formattedResult: AttributedString = string.inflected()
return formattedResult
}
This provides the correct string. But I'm not sure this is the correct way to do it because I have to manually specify the morphology.number (I guess .pluralFew and .pluralMany depends on the language).
Is there a better solution?
Topic:
Programming Languages
SubTopic:
Swift
Tags: