You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
8.2 KiB

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