diff --git a/src/Symfony/Component/Translation/MessageSelector.php b/src/Symfony/Component/Translation/MessageSelector.php index fa976e63da97..79c580239e6e 100644 --- a/src/Symfony/Component/Translation/MessageSelector.php +++ b/src/Symfony/Component/Translation/MessageSelector.php @@ -58,8 +58,9 @@ public function choose($message, $number, $locale) if (preg_match('/^(?P'.Interval::getIntervalRegexp().')\s*(?P.*?)$/x', $part, $matches)) { $explicitRules[$matches['interval']] = $matches['message']; - } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) { - $standardRules[] = $matches[1]; + } elseif (preg_match('/^(?P\w+)\:\s*(?P.*?)$/', $part, $matches)) { + $standardRules[] = $matches['message']; + $standardRules[$matches['tag']] = $matches['message']; } else { $standardRules[] = $part; } @@ -67,12 +68,18 @@ public function choose($message, $number, $locale) // try to match an explicit rule, then fallback to the standard ones foreach ($explicitRules as $interval => $m) { - if (Interval::test($number, $interval)) { + if (Interval::test((int) $number, $interval)) { return $m; } } - $position = PluralizationRules::get($number, $locale); + // try to match an explicit tag rule + if (!is_integer($number) && isset($standardRules[$number])) { + return $standardRules[$number]; + } + + $position = PluralizationRules::get((int) $number, $locale); + if (!isset($standardRules[$position])) { throw new \InvalidArgumentException(sprintf('Unable to choose a translation for "%s" with locale "%s". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale)); } diff --git a/src/Symfony/Component/Translation/Tests/MessageSelectorTest.php b/src/Symfony/Component/Translation/Tests/MessageSelectorTest.php index 63ee8676d7ac..a2166e1ed11e 100644 --- a/src/Symfony/Component/Translation/Tests/MessageSelectorTest.php +++ b/src/Symfony/Component/Translation/Tests/MessageSelectorTest.php @@ -56,6 +56,10 @@ public function getChooseTests() array('There is one apple', 'one: There is one apple|more: There is %count% apples', 1), array('There is %count% apples', 'one: There is one apple|more: There is %count% apples', 10), + array('There is %count% apples', 'one: There is one apple|more: There is %count% apples', 'more'), + array('There is one apple', 'one: There is one apple|more: There is %count% apples', 'one'), + array('There are some apples', 'one: There is one apple|some: There are some apples|more: There is %count% apples', 'some'), + array('There is no apples', '{0} There is no apples|one: There is one apple|more: There is %count% apples', 0), array('There is one apple', '{0} There is no apples|one: There is one apple|more: There is %count% apples', 1), array('There is %count% apples', '{0} There is no apples|one: There is one apple|more: There is %count% apples', 10), diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index 4db0be230b3a..3402af0d10ec 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -155,7 +155,7 @@ public function transChoice($id, $number, array $parameters = array(), $domain = } } - return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters); + return strtr($this->selector->choose($catalogue->get($id, $domain), $number, $locale), $parameters); } protected function loadCatalogue($locale) diff --git a/src/Symfony/Component/Translation/TranslatorInterface.php b/src/Symfony/Component/Translation/TranslatorInterface.php index 2d75cf6aa8c7..23ee93dd6ec8 100644 --- a/src/Symfony/Component/Translation/TranslatorInterface.php +++ b/src/Symfony/Component/Translation/TranslatorInterface.php @@ -38,7 +38,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale * Translates the given choice message by choosing a translation according to a number. * * @param string $id The message id - * @param integer $number The number to use to find the indice of the message + * @param mixed $number The number or tag to use to find the indice of the message * @param array $parameters An array of parameters for the message * @param string $domain The domain for the message * @param string $locale The locale pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy