bdbb0f965fff0da82c75c9b5a385f28f135df07e
[yaffs-website] / vendor / symfony / translation / MessageSelector.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation;
13
14 /**
15  * MessageSelector.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  * @author Bernhard Schussek <bschussek@gmail.com>
19  */
20 class MessageSelector
21 {
22     /**
23      * Given a message with different plural translations separated by a
24      * pipe (|), this method returns the correct portion of the message based
25      * on the given number, locale and the pluralization rules in the message
26      * itself.
27      *
28      * The message supports two different types of pluralization rules:
29      *
30      * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
31      * indexed:  There is one apple|There are %count% apples
32      *
33      * The indexed solution can also contain labels (e.g. one: There is one apple).
34      * This is purely for making the translations more clear - it does not
35      * affect the functionality.
36      *
37      * The two methods can also be mixed:
38      *     {0} There are no apples|one: There is one apple|more: There are %count% apples
39      *
40      * @param string $message The message being translated
41      * @param int    $number  The number of items represented for the message
42      * @param string $locale  The locale to use for choosing
43      *
44      * @return string
45      *
46      * @throws \InvalidArgumentException
47      */
48     public function choose($message, $number, $locale)
49     {
50         $parts = explode('|', $message);
51         $explicitRules = array();
52         $standardRules = array();
53         foreach ($parts as $part) {
54             $part = trim($part);
55
56             if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
57                 $explicitRules[$matches['interval']] = $matches['message'];
58             } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
59                 $standardRules[] = $matches[1];
60             } else {
61                 $standardRules[] = $part;
62             }
63         }
64
65         // try to match an explicit rule, then fallback to the standard ones
66         foreach ($explicitRules as $interval => $m) {
67             if (Interval::test($number, $interval)) {
68                 return $m;
69             }
70         }
71
72         $position = PluralizationRules::get($number, $locale);
73
74         if (!isset($standardRules[$position])) {
75             // when there's exactly one rule given, and that rule is a standard
76             // rule, use this rule
77             if (1 === count($parts) && isset($standardRules[0])) {
78                 return $standardRules[0];
79             }
80
81             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));
82         }
83
84         return $standardRules[$position];
85     }
86 }