Xcode 15 strings catalog plurals without format

571 Views Asked by At

In stringsdict it was possible to define a format separately from the translation so you could do something like "picker.days" which you could pass in an integer and it would return you only "Day" or "Days" without the actual value in it.

Now with the strings catalog it seems like the "%d" HAS to be in the value, otherwise it's not recognized. Is there now no way to pluralize anything without having the actual value inside the translation?

Stringsdict

Strings catalog

1

There are 1 best solutions below

2
Alexander On

I've asked the same question at the Apple Developer support forums and I received this answer:

Hi, Stringsdict and String Catalog Plural Variants are not suited for a string that has plurality but no number associated with that plural. This would do the right thing in English, but many languages would have issues. The plural rules are different between number-based plurals (one, two, few many, other) and non-number-based plurals (1, 2+). It’s recommended here to have 2 different plain strings, controlled by if count == 1 { … } else { … }. More information in this session at the time linked: https://developer.apple.com/wwdc21/10221?time=1330

I have decided to use this workaround for my project

My localized strings are formatted as:

One: %d~day
Other: %d~days

And then I basically split the localized string by ~ symbol:

let pairs = String(localized: "clock-timer.label.days \(daysCount)")
        .split(separator: "~")
        .map { String($0) }

let number = pairs[0] // here is the number of days
let text = pairs[1] // here is the localized word (day, days, etc.)

And later you can show you words to user:

VStack {
  Text(verbatim: number)
  Text(verbatim: text)
}