Automatic Grammar Agreement with formatted number: use integer value to switch categories

Hello,

I want to use Automatic Grammar Agreement to localise a string in my app, let say "three remaining activities". The string "three" is obtained by using a NumberFormatter with a numberStyle set to .spellOut (so I'm not using an Integer)

var formatter: NumberFormatter = NumberFormatter()
formatter.numberStyle = .spellOut

let formattedCount: String = numberFormatter.string(from: count as NSNumber)!

Text("key_with_string_\(formattedCount)")

In my string catalog, I have translated the key key_with_string_%@ like this ^[%@ remaining activity](inflect: true), but it does not work.

I've tried to add the integer value used by the number formatter in the key key_with_string_%@_%lld but it does not work.

  1. Should Automatic Grammar Agreement work normally just by using the formatted string provided by the NumberFormatter?
  2. If not, is there a way to specify to use a secondary variable (my count integer) to switch between different categories like one and other automatically?

Thanks ! Axel

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?

Automatic Grammar Agreement with formatted number: use integer value to switch categories
 
 
Q