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