Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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\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 <fabien@symfony.com> |
| 20 | */ |
| 21 | trait TranslatorTrait |
| 22 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 23 | private ?string $locale = null; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public function setLocale(string $locale) |
| 29 | { |
| 30 | $this->locale = $locale; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * {@inheritdoc} |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 35 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 36 | public function getLocale(): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 37 | { |
| 38 | return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en'); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * {@inheritdoc} |
| 43 | */ |
| 44 | public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string |
| 45 | { |
| 46 | if (null === $id || '' === $id) { |
| 47 | return ''; |
| 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 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 143 | switch ('pt_BR' !== $locale && 'en_US_POSIX' !== $locale && \strlen($locale) > 3 ? substr($locale, 0, strrpos($locale, '_')) : $locale) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 144 | case 'af': |
| 145 | case 'bn': |
| 146 | case 'bg': |
| 147 | case 'ca': |
| 148 | case 'da': |
| 149 | case 'de': |
| 150 | case 'el': |
| 151 | case 'en': |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 152 | case 'en_US_POSIX': |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 153 | case 'eo': |
| 154 | case 'es': |
| 155 | case 'et': |
| 156 | case 'eu': |
| 157 | case 'fa': |
| 158 | case 'fi': |
| 159 | case 'fo': |
| 160 | case 'fur': |
| 161 | case 'fy': |
| 162 | case 'gl': |
| 163 | case 'gu': |
| 164 | case 'ha': |
| 165 | case 'he': |
| 166 | case 'hu': |
| 167 | case 'is': |
| 168 | case 'it': |
| 169 | case 'ku': |
| 170 | case 'lb': |
| 171 | case 'ml': |
| 172 | case 'mn': |
| 173 | case 'mr': |
| 174 | case 'nah': |
| 175 | case 'nb': |
| 176 | case 'ne': |
| 177 | case 'nl': |
| 178 | case 'nn': |
| 179 | case 'no': |
| 180 | case 'oc': |
| 181 | case 'om': |
| 182 | case 'or': |
| 183 | case 'pa': |
| 184 | case 'pap': |
| 185 | case 'ps': |
| 186 | case 'pt': |
| 187 | case 'so': |
| 188 | case 'sq': |
| 189 | case 'sv': |
| 190 | case 'sw': |
| 191 | case 'ta': |
| 192 | case 'te': |
| 193 | case 'tk': |
| 194 | case 'ur': |
| 195 | case 'zu': |
| 196 | return (1 == $number) ? 0 : 1; |
| 197 | |
| 198 | case 'am': |
| 199 | case 'bh': |
| 200 | case 'fil': |
| 201 | case 'fr': |
| 202 | case 'gun': |
| 203 | case 'hi': |
| 204 | case 'hy': |
| 205 | case 'ln': |
| 206 | case 'mg': |
| 207 | case 'nso': |
| 208 | case 'pt_BR': |
| 209 | case 'ti': |
| 210 | case 'wa': |
| 211 | return ($number < 2) ? 0 : 1; |
| 212 | |
| 213 | case 'be': |
| 214 | case 'bs': |
| 215 | case 'hr': |
| 216 | case 'ru': |
| 217 | case 'sh': |
| 218 | case 'sr': |
| 219 | case 'uk': |
| 220 | return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); |
| 221 | |
| 222 | case 'cs': |
| 223 | case 'sk': |
| 224 | return (1 == $number) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2); |
| 225 | |
| 226 | case 'ga': |
| 227 | return (1 == $number) ? 0 : ((2 == $number) ? 1 : 2); |
| 228 | |
| 229 | case 'lt': |
| 230 | return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); |
| 231 | |
| 232 | case 'sl': |
| 233 | return (1 == $number % 100) ? 0 : ((2 == $number % 100) ? 1 : (((3 == $number % 100) || (4 == $number % 100)) ? 2 : 3)); |
| 234 | |
| 235 | case 'mk': |
| 236 | return (1 == $number % 10) ? 0 : 1; |
| 237 | |
| 238 | case 'mt': |
| 239 | return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3)); |
| 240 | |
| 241 | case 'lv': |
| 242 | return (0 == $number) ? 0 : (((1 == $number % 10) && (11 != $number % 100)) ? 1 : 2); |
| 243 | |
| 244 | case 'pl': |
| 245 | return (1 == $number) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2); |
| 246 | |
| 247 | case 'cy': |
| 248 | return (1 == $number) ? 0 : ((2 == $number) ? 1 : (((8 == $number) || (11 == $number)) ? 2 : 3)); |
| 249 | |
| 250 | case 'ro': |
| 251 | return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2); |
| 252 | |
| 253 | case 'ar': |
| 254 | return (0 == $number) ? 0 : ((1 == $number) ? 1 : ((2 == $number) ? 2 : ((($number % 100 >= 3) && ($number % 100 <= 10)) ? 3 : ((($number % 100 >= 11) && ($number % 100 <= 99)) ? 4 : 5)))); |
| 255 | |
| 256 | default: |
| 257 | return 0; |
| 258 | } |
| 259 | } |
| 260 | } |