PluralizationRules.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\Component\Translation;
  11. /**
  12. * Returns the plural rules for a given locale.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @deprecated since Symfony 4.2, use IdentityTranslator instead
  17. */
  18. class PluralizationRules
  19. {
  20. private static $rules = [];
  21. /**
  22. * Returns the plural position to use for the given locale and number.
  23. *
  24. * @param int $number The number
  25. * @param string $locale The locale
  26. *
  27. * @return int The plural position
  28. */
  29. public static function get($number, $locale/*, bool $triggerDeprecation = true*/)
  30. {
  31. if (3 > \func_num_args() || func_get_arg(2)) {
  32. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
  33. }
  34. if ('pt_BR' === $locale) {
  35. // temporary set a locale for brazilian
  36. $locale = 'xbr';
  37. }
  38. if (\strlen($locale) > 3) {
  39. $locale = substr($locale, 0, -\strlen(strrchr($locale, '_')));
  40. }
  41. if (isset(self::$rules[$locale])) {
  42. $return = self::$rules[$locale]($number);
  43. if (!\is_int($return) || $return < 0) {
  44. return 0;
  45. }
  46. return $return;
  47. }
  48. /*
  49. * The plural rules are derived from code of the Zend Framework (2010-09-25),
  50. * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
  51. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  52. */
  53. switch ($locale) {
  54. case 'az':
  55. case 'bo':
  56. case 'dz':
  57. case 'id':
  58. case 'ja':
  59. case 'jv':
  60. case 'ka':
  61. case 'km':
  62. case 'kn':
  63. case 'ko':
  64. case 'ms':
  65. case 'th':
  66. case 'tr':
  67. case 'vi':
  68. case 'zh':
  69. return 0;
  70. case 'af':
  71. case 'bn':
  72. case 'bg':
  73. case 'ca':
  74. case 'da':
  75. case 'de':
  76. case 'el':
  77. case 'en':
  78. case 'eo':
  79. case 'es':
  80. case 'et':
  81. case 'eu':
  82. case 'fa':
  83. case 'fi':
  84. case 'fo':
  85. case 'fur':
  86. case 'fy':
  87. case 'gl':
  88. case 'gu':
  89. case 'ha':
  90. case 'he':
  91. case 'hu':
  92. case 'is':
  93. case 'it':
  94. case 'ku':
  95. case 'lb':
  96. case 'ml':
  97. case 'mn':
  98. case 'mr':
  99. case 'nah':
  100. case 'nb':
  101. case 'ne':
  102. case 'nl':
  103. case 'nn':
  104. case 'no':
  105. case 'oc':
  106. case 'om':
  107. case 'or':
  108. case 'pa':
  109. case 'pap':
  110. case 'ps':
  111. case 'pt':
  112. case 'so':
  113. case 'sq':
  114. case 'sv':
  115. case 'sw':
  116. case 'ta':
  117. case 'te':
  118. case 'tk':
  119. case 'ur':
  120. case 'zu':
  121. return (1 == $number) ? 0 : 1;
  122. case 'am':
  123. case 'bh':
  124. case 'fil':
  125. case 'fr':
  126. case 'gun':
  127. case 'hi':
  128. case 'hy':
  129. case 'ln':
  130. case 'mg':
  131. case 'nso':
  132. case 'xbr':
  133. case 'ti':
  134. case 'wa':
  135. return ((0 == $number) || (1 == $number)) ? 0 : 1;
  136. case 'be':
  137. case 'bs':
  138. case 'hr':
  139. case 'ru':
  140. case 'sh':
  141. case 'sr':
  142. case 'uk':
  143. return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
  144. case 'cs':
  145. case 'sk':
  146. return (1 == $number) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2);
  147. case 'ga':
  148. return (1 == $number) ? 0 : ((2 == $number) ? 1 : 2);
  149. case 'lt':
  150. return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
  151. case 'sl':
  152. return (1 == $number % 100) ? 0 : ((2 == $number % 100) ? 1 : (((3 == $number % 100) || (4 == $number % 100)) ? 2 : 3));
  153. case 'mk':
  154. return (1 == $number % 10) ? 0 : 1;
  155. case 'mt':
  156. return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3));
  157. case 'lv':
  158. return (0 == $number) ? 0 : (((1 == $number % 10) && (11 != $number % 100)) ? 1 : 2);
  159. case 'pl':
  160. return (1 == $number) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2);
  161. case 'cy':
  162. return (1 == $number) ? 0 : ((2 == $number) ? 1 : (((8 == $number) || (11 == $number)) ? 2 : 3));
  163. case 'ro':
  164. return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2);
  165. case 'ar':
  166. 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))));
  167. default:
  168. return 0;
  169. }
  170. }
  171. /**
  172. * Overrides the default plural rule for a given locale.
  173. *
  174. * @param callable $rule A PHP callable
  175. * @param string $locale The locale
  176. */
  177. public static function set(callable $rule, $locale)
  178. {
  179. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
  180. if ('pt_BR' === $locale) {
  181. // temporary set a locale for brazilian
  182. $locale = 'xbr';
  183. }
  184. if (\strlen($locale) > 3) {
  185. $locale = substr($locale, 0, -\strlen(strrchr($locale, '_')));
  186. }
  187. self::$rules[$locale] = $rule;
  188. }
  189. }