symfony: [Translation] Floating number plural broken

Symfony version(s) affected: 4.2.3

Description
Decimal number between 1 and 2 produces incorrect plural in English.

How to reproduce


$t = new \Symfony\Component\Translation\Translator('en');

$t->addLoader('array', new \Symfony\Component\Translation\Loader\ArrayLoader());
$t->addResource('array', [
    'liter' => '%count% liter|%count% liters',
], 'en');

echo $t->trans('liter', ['%count%' => 1.5]);

echo "\n";

$t = new \Symfony\Component\Translation\Translator('fr');

$t->addLoader('array', new \Symfony\Component\Translation\Loader\ArrayLoader());
$t->addResource('array', [
    'liter' => '%count% litre|%count% litres',
], 'fr');

echo $t->trans('liter', ['%count%' => 1.5]);

It outputs:

1.5 liter
1.5 litre

It should output:

1.5 liters
1.5 litre

Possible Solution int typing here should be removed, it’s a float, numbers should not be truncated: https://github.com/symfony/symfony/blob/32aa969dfff52adfc8f8efac8e7cd21b719f9ac0/src/Symfony/Component/Translation/IdentityTranslator.php#L57

Related to #16256

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (17 by maintainers)

Commits related to this issue

Most upvoted comments

I just tested this issue, with translations in ICU format, and I do not find the bug.

{{ 'product.carats'|trans({ 'carat': 1.2 }) }}

With this translation:

<unit id="zcajhWf" name="product.carats">
  <segment>
    <source>product.carats</source>
    <target>{carat, plural,
      one   {Weight: # carat}
      other {Weight: # carats}
    }</target>
  </segment>
</unit>

The output is correct:

Weight: 1.2 carats

The interface defined that the number had to be an integer: https://github.com/symfony/symfony/blob/13d1d7673a1f309ae97d26b8830781161b0af52b/src/Symfony/Component/Translation/TranslatorInterface.php#L37-L50

Since I have no idea if this works for all supported I would not be in favour of adding this feature.

Your use case only used to work accidentally based on the fact that we were not able to use scalar type hints on older Symfony versions. The count value always was expected to be an integer. Decimals are currently not supported.