Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Translator] fix handling plural for floating numbers
  • Loading branch information
kylekatarnls authored and nicolas-grekas committed Jan 26, 2021
commit 533cd7ef6c2019ae2d562e13f03f6c204d6fb9bd
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