Cardinal numbers with NumberFormatter using Locale settings in Swift
Asked Answered
L

0

6

How can I generate cardinal numbers(One, Two, Three) using NumberFormatter in Swift using Locale settings.

For example, I am able to convert 1 minute -> One minute using English Locale settings. But for Norwegian it should be Et minutt, While it translates to En minutt which is wrong. Norwegian translation for One is En but when we use it in some sentence, sometimes it should be Et. Here is sample of my code.

let measurement = Measurement(value: 1, unit: UnitDuration.minutes)
let measurementFormatter = MeasurementFormatter()
measurementFormatter.unitStyle = .long
measurementFormatter.locale = Locale(identifier: "nb_no")
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "nb_no")
formatter.numberStyle = .spellOut
measurementFormatter.numberFormatter = formatter
let result = measurementFormatter.string(from: measurement)

Output:

én minutt

Expected Output

et minutt

Lydie answered 4/1, 2018 at 12:0 Comment(6)
Possibly helpful: #42746621.Outline
Yeah this can solve my problem for now but I was looking for some solution using Formatters so that my solution can be a generic one.Lydie
Same with the German language: "eins Minute" instead of the correct "Eine Minute". – Interesting problem!Outline
Same with russian language: "один минута" instead of "одна минута", and "два минуты" instead of "две минуты". And ukrainian and belarusian too. Formatting for 1 and 2 is wrong.Palfrey
My guess would be that the MeasurementFormatter in combination with spellout style does not support correct gender/plural, and the "Plural Rule Properties" in a stringsdict file are your only choice. See e.g. "Plural and Gender Support" in objc.io/issues/9-strings/string-localizationOutline
DateComponentsFormatter has the same problem.Outline

© 2022 - 2024 — McMap. All rights reserved.