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
- Add test to show #30215 issue — committed to kylekatarnls/symfony by kylekatarnls 3 years ago
- Add test to show #30215 issue — committed to kylekatarnls/symfony by kylekatarnls 3 years ago
- Fix #30215 handle plural for floating numbers — committed to kylekatarnls/symfony by kylekatarnls 3 years ago
- Fix #30215 handle plural for floating numbers — committed to kylekatarnls/symfony by kylekatarnls 3 years ago
- Fix #30215 handle plural for floating numbers — committed to kylekatarnls/symfony by kylekatarnls 3 years ago
- Fix #30215 handle plural for negative numbers — committed to kylekatarnls/symfony by kylekatarnls 3 years ago
- bug #39887 [Translator] fix handling plural for floating numbers (kylekatarnls) This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Translator] fix handling plural ... — committed to symfony/symfony by nicolas-grekas 3 years ago
I just tested this issue, with translations in ICU format, and I do not find the bug.
With this translation:
The output is correct:
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.