Skip to content

Implemented tagging support for translation. #5547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 11 additions & 4 deletions src/Symfony/Component/Translation/MessageSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,28 @@ public function choose($message, $number, $locale)

if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/x', $part, $matches)) {
$explicitRules[$matches['interval']] = $matches['message'];
} elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
$standardRules[] = $matches[1];
} elseif (preg_match('/^(?P<tag>\w+)\:\s*(?P<message>.*?)$/', $part, $matches)) {
$standardRules[] = $matches['message'];
$standardRules[$matches['tag']] = $matches['message'];
} else {
$standardRules[] = $part;
}
}

// 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/TranslatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documenting it as integer|string would be more precise than mixed

* @param array $parameters An array of parameters for the message
* @param string $domain The domain for the message
* @param string $locale The locale
Expand Down