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 for floating numbers
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix #30215
| License | MIT
Commits
-------
533cd7ef6c
[Translator] fix handling plural for floating numbers
This commit is contained in:
commit
4b8983aef5
@ -70,7 +70,7 @@ class IdentityTranslator implements LegacyTranslatorInterface, TranslatorInterfa
|
|||||||
return $this->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
|
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);
|
return PluralizationRules::get($number, $locale, false);
|
||||||
}
|
}
|
||||||
|
@ -25,13 +25,15 @@ class PluralizationRules
|
|||||||
/**
|
/**
|
||||||
* Returns the plural position to use for the given locale and number.
|
* 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
|
* @param string $locale The locale
|
||||||
*
|
*
|
||||||
* @return int The plural position
|
* @return int The plural position
|
||||||
*/
|
*/
|
||||||
public static function get($number, $locale/*, bool $triggerDeprecation = true*/)
|
public static function get($number, $locale/*, bool $triggerDeprecation = true*/)
|
||||||
{
|
{
|
||||||
|
$number = abs($number);
|
||||||
|
|
||||||
if (3 > \func_num_args() || func_get_arg(2)) {
|
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);
|
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), \E_USER_DEPRECATED);
|
||||||
}
|
}
|
||||||
@ -144,7 +146,7 @@ class PluralizationRules
|
|||||||
case 'xbr':
|
case 'xbr':
|
||||||
case 'ti':
|
case 'ti':
|
||||||
case 'wa':
|
case 'wa':
|
||||||
return ((0 == $number) || (1 == $number)) ? 0 : 1;
|
return ($number < 2) ? 0 : 1;
|
||||||
|
|
||||||
case 'be':
|
case 'be':
|
||||||
case 'bs':
|
case 'bs':
|
||||||
|
@ -612,6 +612,18 @@ class TranslatorTest extends TestCase
|
|||||||
|
|
||||||
// Override %count% with a custom value
|
// 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', ''],
|
['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', ''],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,8 +136,10 @@ EOF;
|
|||||||
* which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
|
* 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)
|
* 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) {
|
switch ('pt_BR' !== $locale && \strlen($locale) > 3 ? substr($locale, 0, strrpos($locale, '_')) : $locale) {
|
||||||
case 'af':
|
case 'af':
|
||||||
case 'bn':
|
case 'bn':
|
||||||
@ -205,7 +207,7 @@ EOF;
|
|||||||
case 'pt_BR':
|
case 'pt_BR':
|
||||||
case 'ti':
|
case 'ti':
|
||||||
case 'wa':
|
case 'wa':
|
||||||
return ((0 == $number) || (1 == $number)) ? 0 : 1;
|
return ($number < 2) ? 0 : 1;
|
||||||
|
|
||||||
case 'be':
|
case 'be':
|
||||||
case 'bs':
|
case 'bs':
|
||||||
|
Reference in New Issue
Block a user