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