Aucune description

PluralizationRules.php 6.4KB

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