4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation
;
17 * @author Fabien Potencier <fabien@symfony.com>
24 * Given a message with different plural translations separated by a
25 * pipe (|), this method returns the correct portion of the message based
26 * on the given number, locale and the pluralization rules in the message
29 * The message supports two different types of pluralization rules:
31 * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
32 * indexed: There is one apple|There are %count% apples
34 * The indexed solution can also contain labels (e.g. one: There is one apple).
35 * This is purely for making the translations more clear - it does not
36 * affect the functionality.
38 * The two methods can also be mixed:
39 * {0} There are no apples|one: There is one apple|more: There are %count% apples
41 * @param string $message The message being translated
42 * @param integer $number The number of items represented for the message
43 * @param string $locale The locale to use for choosing
47 * @throws \InvalidArgumentException
51 public function choose($message, $number, $locale)
53 $parts = explode('|', $message);
54 $explicitRules = array();
55 $standardRules = array();
56 foreach ($parts as $part) {
59 if (preg_match('/^(?P<interval>'.Interval
::getIntervalRegexp().')\s*(?P<message>.*?)$/x', $part, $matches)) {
60 $explicitRules[$matches['interval']] = $matches['message'];
61 } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
62 $standardRules[] = $matches[1];
64 $standardRules[] = $part;
68 // try to match an explicit rule, then fallback to the standard ones
69 foreach ($explicitRules as $interval => $m) {
70 if (Interval
::test($number, $interval)) {
75 $position = PluralizationRules
::get($number, $locale);
76 if (!isset($standardRules[$position])) {
77 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));
80 return $standardRules[$position];