TranslatorTrait.php 8.1 KB

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