Replies: 2 comments
-
The C API of ICU4C has only those two functions you referenced, and only the second one supports trailing zeros, which requires invoking NumberFormat. In ICU4X, we address this problem much more elegantly with FixedDecimal. I would like if we could stick with the FixedDecimal input instead of PluralOperands over FFI, as well as possibly the convenience functions for int and string. (Follow-up: #811) More specifically:
Fine with me as a convenience function.
No, because passing floats to PluralRules is misguided. #811
I see your point, but this isn't the right way to implement a "selectFormatted" because:
Note that the ICU4C function takes in a "UFormattedNumber", which is roughly equivalent to our "FormattedFixedDecimal", except that I think in ICU4X we should probably stick with FixedDecimal for this purpose. Note: I have worked extensively on ICU4C PluralRules, and am responsible for both UFormattedNumber as well as the uplrules_selectFormatted() function you referenced. I want to make a faster and more modular solution in ICU4X via FixedDecimal, which is one of my first contributions to this project. |
Beta Was this translation helpful? Give feedback.
-
2021-06-17:
|
Beta Was this translation helpful? Give feedback.
-
Internally in ICU4X, our
select()
function takes a type that can be turned into aPluralOperands
struct as an argument. I think this makes a lot of sense, and provides a lot of versatility.Our FFI-exposed
select()
funciton, however, takes aPluralOperands
struct directly (rather than something that can be turned into aPluralOperands
).The
PluralOperands
struct represents a numeric value in a very specific format, and this puts a lot of burden on users to be able to have their own functionality that can convert integer, floating-point, or string types into ourPluralOperands
format.While testing ICU4X in Gecko, I decided it would be easiest to expose the
PluralOperands::from_str()
function over the FFI, so that I could easily create aPluralOperands
object from a string representation of adouble
that I get back fromNumberFormat
.While this is working fine for me to test our code in Gecko, I do not think that this is the final way our API should work. Currently, the easiest way to invoke
select()
across FFI is to either write your own logic to convert a number into ourPluralOperands
format, or toPluralOperands
object from your string.select()
and pass in yourPluralOperands
object.Our current implementation also has the quality that
PluralOperands::from_str()
only works on string representations of numbers that use ASCII digits with no separators and a'.'
decimal, e.g.123456.789
. Some locales may format this as123.456,789
("es"
) or123 456.789
("br"
), or١٢٣٬٤٥٦٫٧٨٩
("ar"
).Currently, in Gecko, regardless of my
PluralRules
locale, I am having to callNumberFormat::format()
specifically with the"en"
locale, with separators turned off, so that I can pass that formatted number to ICU4X to turn intoPluralOperands
.I feel like we should expose more direct ways to call
select()
over FFI that put the burden of creating thePluralOperands
object on the internal implementation details, rather than on the caller.ICU4XPluralCategory select(<integer type>)
ICU4XPluralCategory select(<float type>)
ICU4XPluralCategory selectFormatted(UTF8String)
Where
selectFormatted()
would be able to accept all of"123456.789"
,"123,456.789"
,"123.456,789"
,"123 456.789"
,"١٢٣٬٤٥٦٫٧٨٩"
etc. and still give you back the correctPluralCategory
.ICU4C exposes
select()
in a similar fashion:uplrules_select()
uplrules_selectFormatted()
The two
select()
methods with numeric types could be exposed right away, but theselectFormatted()
function would require adding functionality to ICU4X that could turn a formatted string into aPluralOperands
internally.Beta Was this translation helpful? Give feedback.
All reactions