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;
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
19 * @author Fabien Potencier <fabien@symfony.com>
20 * @author Bernhard Schussek <bschussek@gmail.com>
25 * Given a message with different plural translations separated by a
26 * pipe (|), this method returns the correct portion of the message based
27 * on the given number, locale and the pluralization rules in the message
30 * The message supports two different types of pluralization rules:
32 * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
33 * indexed: There is one apple|There are %count% apples
35 * The indexed solution can also contain labels (e.g. one: There is one apple).
36 * This is purely for making the translations more clear - it does not
37 * affect the functionality.
39 * The two methods can also be mixed:
40 * {0} There are no apples|one: There is one apple|more: There are %count% apples
42 * @param string $message The message being translated
43 * @param int $number The number of items represented for the message
44 * @param string $locale The locale to use for choosing
48 * @throws InvalidArgumentException
50 public function choose($message, $number, $locale)
53 if (preg_match('/^\|++$/', $message)) {
54 $parts = explode('|', $message);
55 } elseif (preg_match_all('/(?:\|\||[^\|])++/', $message, $matches)) {
59 $explicitRules = array();
60 $standardRules = array();
61 foreach ($parts as $part) {
62 $part = trim(str_replace('||', '|', $part));
64 if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
65 $explicitRules[$matches['interval']] = $matches['message'];
66 } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
67 $standardRules[] = $matches[1];
69 $standardRules[] = $part;
73 // try to match an explicit rule, then fallback to the standard ones
74 foreach ($explicitRules as $interval => $m) {
75 if (Interval::test($number, $interval)) {
80 $position = PluralizationRules::get($number, $locale);
82 if (!isset($standardRules[$position])) {
83 // when there's exactly one rule given, and that rule is a standard
84 // rule, use this rule
85 if (1 === \count($parts) && isset($standardRules[0])) {
86 return $standardRules[0];
89 throw new InvalidArgumentException(sprintf('Unable to choose a translation for "%s" with locale "%s" for value "%d". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale, $number));
92 return $standardRules[$position];