Skip to content

Commit

Permalink
bug #39887 [Translator] fix handling plural for floating numbers (kyl…
Browse files Browse the repository at this point in the history
…ekatarnls)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Translator] fix handling plural for floating numbers

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #30215
| License       | MIT

Commits
-------

533cd7e [Translator] fix handling plural for floating numbers
  • Loading branch information
nicolas-grekas committed Jan 26, 2021
2 parents aa5ca01 + 533cd7e commit 4b8983a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/IdentityTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function transChoice($id, $number, array $parameters = [], $domain = null
return $this->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
}

private function getPluralizationRule(int $number, string $locale): int
private function getPluralizationRule(float $number, string $locale): int
{
return PluralizationRules::get($number, $locale, false);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Translation/PluralizationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class PluralizationRules
/**
* Returns the plural position to use for the given locale and number.
*
* @param int $number The number
* @param float $number The number
* @param string $locale The locale
*
* @return int The plural position
*/
public static function get($number, $locale/*, bool $triggerDeprecation = true*/)
{
$number = abs($number);

if (3 > \func_num_args() || func_get_arg(2)) {
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), \E_USER_DEPRECATED);
}
Expand Down Expand Up @@ -144,7 +146,7 @@ public static function get($number, $locale/*, bool $triggerDeprecation = true*/
case 'xbr':
case 'ti':
case 'wa':
return ((0 == $number) || (1 == $number)) ? 0 : 1;
return ($number < 2) ? 0 : 1;

case 'be':
case 'bs':
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,18 @@ public function getTransChoiceTests()

// Override %count% with a custom value
['Il y a quelques pommes', 'one: There is one apple|more: There are %count% apples', 'one: Il y a %count% pomme|more: Il y a quelques pommes', 2, ['%count%' => 'quelques'], 'fr', ''],

// Floating values
['1.5 liters', 'key', '%count% liter|%count% liters', 1.5, ['%count%' => 1.5], 'en', ''],
['1.5 litre', 'key', '%count% litre|%count% litres', 1.5, ['%count%' => 1.5], 'fr', ''],

// Negative values
['-1 degree', 'key', '%count% degree|%count% degrees', -1, ['%count%' => -1], 'en', ''],
['-1 degré', 'key', '%count% degré|%count% degrés', -1, ['%count%' => -1], 'fr', ''],
['-1.5 degrees', 'key', '%count% degree|%count% degrees', -1.5, ['%count%' => -1.5], 'en', ''],
['-1.5 degré', 'key', '%count% degré|%count% degrés', -1.5, ['%count%' => -1.5], 'fr', ''],
['-2 degrees', 'key', '%count% degree|%count% degrees', -2, ['%count%' => -2], 'en', ''],
['-2 degrés', 'key', '%count% degré|%count% degrés', -2, ['%count%' => -2], 'fr', ''],
];
}

Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Contracts/Translation/TranslatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ public function trans($id, array $parameters = [], $domain = null, $locale = nul
* which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
* Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
*/
private function getPluralizationRule(int $number, string $locale): int
private function getPluralizationRule(float $number, string $locale): int
{
$number = abs($number);

switch ('pt_BR' !== $locale && \strlen($locale) > 3 ? substr($locale, 0, strrpos($locale, '_')) : $locale) {
case 'af':
case 'bn':
Expand Down Expand Up @@ -205,7 +207,7 @@ private function getPluralizationRule(int $number, string $locale): int
case 'pt_BR':
case 'ti':
case 'wa':
return ((0 == $number) || (1 == $number)) ? 0 : 1;
return ($number < 2) ? 0 : 1;

case 'be':
case 'bs':
Expand Down

0 comments on commit 4b8983a

Please sign in to comment.