Skip to content

Selectors docs regarding use of NUMBER are incorrect (and a bad idea) #155

Open
@spookylukey

Description

@spookylukey

Here:

https://projectfluent.org/fluent/guide/selectors.html

If the translation requires a number to be formatted in a particular non-default manner, the selector should use the same formatting options. The formatted number will then be used to choose the correct CLDR plural category which, for some languages, might be different than the category of the unformatted number:

your-score =
   { NUMBER($score, minimumFractionDigits: 1) ->
       [0.0]   You scored zero points. What happened?
      *[other] You scored { NUMBER($score, minimumFractionDigits: 1) } points.
   }

The text claims that the formatted version of $score will be used to do the matching. In reality this doesn't happen in the reference implementation - see:

The 0.0 is parsed as a number literal, converted to a FluentNumber, and then compared by value to the FluentNumber instance that results from the first NUMBER call. I added a test to the suite to confirm this, and it makes no difference if you change 0.0 to 0 or 0.00, or if you increase/decrease the value of minimumFractionDigits etc - it is doing a numerical comparison, not taking into account the formatting options.

Further, the behaviour described in the docs would be a bad idea, AFAICS:

  1. Number formatting can be quite expensive, we want to avoid it for selector matching.

  2. If the formatted number should appear in the variant key expression, this presumably means it needs to be changed by translators e.g. in the example, 0.0 when considered as a string matches the formatted NUMBER expression for English, but it would have to be changed to 0,0 for German etc. I'm guessing translators would have confusion over whether to change these numbers like this.

  3. If the formatted number should appear in the variant key expression, all kinds of confusion occurs in terms of parsing/interpreting the FTL. For English locales, the formatted number looks like the number literal for small numbers (1, 2.3 etc.), but not for large numbers (e.g. 1,000 which is not a valid number literal.). For other locales, sometimes the formatted number will again not be a valid number literal, sometimes it might look like a number literal but for a different number (e.g. 1.000 is German for one thousand, but would be parsed as a valid FTL number literal for one).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions