PluralizationRules.php 5.9 KB

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