TranslatorTrait.php 8.1 KB

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