Debian Codegraph @

lib/3rdParty/composer/symfony/translation-contracts/TranslatorTrait.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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\Contracts\Translation;
13
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
15
16 /**
17 * A trait to help implement TranslatorInterface and LocaleAwareInterface.
18 *
19 * @author Fabien Potencier <[email protected]>
20 */
21 trait TranslatorTrait
22 {
23 private ?string $locale = null;
24
25 /**
26 * @return void
27 */
28 public function setLocale(string $locale)
29 {
30 $this->locale = $locale;
31 }
32
33 public function getLocale(): string
34 {
35 return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
36 }
37
38 public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
39 {
40 if (null === $id || '' === $id) {
41 return '';
42 }
43
44 foreach ($parameters as $k => $v) {
45 if ($v instanceof TranslatableInterface) {
46 $parameters[$k] = $v->trans($this, $locale);
47 }
48 }
49
50 if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) {
51 return strtr($id, $parameters);
52 }
53
54 $number = (float) $parameters['%count%'];
55 $locale = $locale ?: $this->getLocale();
56
57 $parts = [];
58 if (preg_match('/^\|++$/', $id)) {
59 $parts = explode('|', $id);
60 } elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) {
61 $parts = $matches[0];
62 }
63
64 $intervalRegexp = <<<'EOF'
65 /^(?P<interval>
66 ({\s*
67 (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
68 \s*})
69
70 |
71
72 (?P<left_delimiter>[\[\]])
73 \s*
74 (?P<left>-Inf|\-?\d+(\.\d+)?)
75 \s*,\s*
76 (?P<right>\+?Inf|\-?\d+(\.\d+)?)
77 \s*
78 (?P<right_delimiter>[\[\]])
79 )\s*(?P<message>.*?)$/xs
80 EOF;
81
82 $standardRules = [];
83 foreach ($parts as $part) {
84 $part = trim(str_replace('||', '|', $part));
85
86 // try to match an explicit rule, then fallback to the standard ones
87 if (preg_match($intervalRegexp, $part, $matches)) {
88 if ($matches[2]) {
89 foreach (explode(',', $matches[3]) as $n) {
90 if ($number == $n) {
91 return strtr($matches['message'], $parameters);
92 }
93 }
94 } else {
95 $leftNumber = '-Inf' === $matches['left'] ? -\INF : (float) $matches['left'];
96 $rightNumber = is_numeric($matches['right']) ? (float) $matches['right'] : \INF;
97
98 if (('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber)
99 && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber)
100 ) {
101 return strtr($matches['message'], $parameters);
102 }
103 }
104 } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
105 $standardRules[] = $matches[1];
106 } else {
107 $standardRules[] = $part;
108 }
109 }
110
111 $position = $this->getPluralizationRule($number, $locale);
112
113 if (!isset($standardRules[$position])) {
114 // when there's exactly one rule given, and that rule is a standard
115 // rule, use this rule
116 if (1 === \count($parts) && isset($standardRules[0])) {
117 return strtr($standardRules[0], $parameters);
118 }
119
120 $message = \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").', $id, $locale, $number);
121
122 if (class_exists(InvalidArgumentException::class)) {
123 throw new InvalidArgumentException($message);
124 }
125
126 throw new \InvalidArgumentException($message);
127 }
128
129 return strtr($standardRules[$position], $parameters);
130 }
131
132 /**
133 * Returns the plural position to use for the given locale and number.
134 *
135 * The plural rules are derived from code of the Zend Framework (2010-09-25),
136 * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
137 * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
138 */
139 private function getPluralizationRule(float $number, string $locale): int
140 {
141 $number = abs($number);
142
143 return match ('pt_BR' !== $locale && 'en_US_POSIX' !== $locale && \strlen($locale) > 3 ? substr($locale, 0, strrpos($locale, '_')) : $locale) {
144 'af',
145 'bn',
146 'bg',
147 'ca',
148 'da',
149 'de',
150 'el',
151 'en',
152 'en_US_POSIX',
153 'eo',
154 'es',
155 'et',
156 'eu',
157 'fa',
158 'fi',
159 'fo',
160 'fur',
161 'fy',
162 'gl',
163 'gu',
164 'ha',
165 'he',
166 'hu',
167 'is',
168 'it',
169 'ku',
170 'lb',
171 'ml',
172 'mn',
173 'mr',
174 'nah',
175 'nb',
176 'ne',
177 'nl',
178 'nn',
179 'no',
180 'oc',
181 'om',
182 'or',
183 'pa',
184 'pap',
185 'ps',
186 'pt',
187 'so',
188 'sq',
189 'sv',
190 'sw',
191 'ta',
192 'te',
193 'tk',
194 'ur',
195 'zu' => (1 == $number) ? 0 : 1,
196 'am',
197 'bh',
198 'fil',
199 'fr',
200 'gun',
201 'hi',
202 'hy',
203 'ln',
204 'mg',
205 'nso',
206 'pt_BR',
207 'ti',
208 'wa' => ($number < 2) ? 0 : 1,
209 'be',
210 'bs',
211 'hr',
212 'ru',
213 'sh',
214 'sr',
215 'uk' => ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2),
216 'cs',
217 'sk' => (1 == $number) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2),
218 'ga' => (1 == $number) ? 0 : ((2 == $number) ? 1 : 2),
219 'lt' => ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2),
220 'sl' => (1 == $number % 100) ? 0 : ((2 == $number % 100) ? 1 : (((3 == $number % 100) || (4 == $number % 100)) ? 2 : 3)),
221 'mk' => (1 == $number % 10) ? 0 : 1,
222 'mt' => (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3)),
223 'lv' => (0 == $number) ? 0 : (((1 == $number % 10) && (11 != $number % 100)) ? 1 : 2),
224 'pl' => (1 == $number) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2),
225 'cy' => (1 == $number) ? 0 : ((2 == $number) ? 1 : (((8 == $number) || (11 == $number)) ? 2 : 3)),
226 'ro' => (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2),
227 'ar' => (0 == $number) ? 0 : ((1 == $number) ? 1 : ((2 == $number) ? 2 : ((($number % 100 >= 3) && ($number % 100 <= 10)) ? 3 : ((($number % 100 >= 11) && ($number % 100 <= 99)) ? 4 : 5)))),
228 default => 0,
229 };
230 }
231 }