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