]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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 | * | |
19 | * @api | |
20 | */ | |
21 | class MessageSelector | |
22 | { | |
23 | /** | |
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 | |
27 | * itself. | |
28 | * | |
29 | * The message supports two different types of pluralization rules: | |
30 | * | |
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 | |
33 | * | |
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. | |
37 | * | |
38 | * The two methods can also be mixed: | |
39 | * {0} There are no apples|one: There is one apple|more: There are %count% apples | |
40 | * | |
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 | |
44 | * | |
45 | * @return string | |
46 | * | |
47 | * @throws \InvalidArgumentException | |
48 | * | |
49 | * @api | |
50 | */ | |
51 | public function choose($message, $number, $locale) | |
52 | { | |
53 | $parts = explode('|', $message); | |
54 | $explicitRules = array(); | |
55 | $standardRules = array(); | |
56 | foreach ($parts as $part) { | |
57 | $part = trim($part); | |
58 | ||
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]; | |
63 | } else { | |
64 | $standardRules[] = $part; | |
65 | } | |
66 | } | |
67 | ||
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)) { | |
71 | return $m; | |
72 | } | |
73 | } | |
74 | ||
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)); | |
78 | } | |
79 | ||
80 | return $standardRules[$position]; | |
81 | } | |
82 | } |