Просмотр файла vendor/symfony/translation-contracts/TranslatorTrait.php

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