PluralizationRules.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // @codeCoverageIgnoreStart
  19. private static $rules = array();
  20. /**
  21. * Returns the plural position to use for the given locale and number.
  22. *
  23. * @param integer $number The number
  24. * @param string $locale The locale
  25. *
  26. * @return integer The plural position
  27. */
  28. public static function get($number, $locale)
  29. {
  30. if ("pt_BR" == $locale) {
  31. // temporary set a locale for brazilian
  32. $locale = "xbr";
  33. }
  34. if (strlen($locale) > 3) {
  35. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  36. }
  37. if (isset(self::$rules[$locale])) {
  38. $return = call_user_func(self::$rules[$locale], $number);
  39. if (!is_int($return) || $return < 0) {
  40. return 0;
  41. }
  42. return $return;
  43. }
  44. /*
  45. * The plural rules are derived from code of the Zend Framework (2010-09-25),
  46. * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
  47. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  48. */
  49. switch ($locale) {
  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 'az':
  68. case 'bn':
  69. case 'bg':
  70. case 'ca':
  71. case 'da':
  72. case 'de':
  73. case 'el':
  74. case 'en':
  75. case 'eo':
  76. case 'es':
  77. case 'et':
  78. case 'eu':
  79. case 'fa':
  80. case 'fi':
  81. case 'fo':
  82. case 'fur':
  83. case 'fy':
  84. case 'gl':
  85. case 'gu':
  86. case 'ha':
  87. case 'he':
  88. case 'hu':
  89. case 'is':
  90. case 'it':
  91. case 'ku':
  92. case 'lb':
  93. case 'ml':
  94. case 'mn':
  95. case 'mr':
  96. case 'nah':
  97. case 'nb':
  98. case 'ne':
  99. case 'nl':
  100. case 'nn':
  101. case 'no':
  102. case 'om':
  103. case 'or':
  104. case 'pa':
  105. case 'pap':
  106. case 'ps':
  107. case 'pt':
  108. case 'so':
  109. case 'sq':
  110. case 'sv':
  111. case 'sw':
  112. case 'ta':
  113. case 'te':
  114. case 'tk':
  115. case 'ur':
  116. case 'zu':
  117. return ($number == 1) ? 0 : 1;
  118. case 'am':
  119. case 'bh':
  120. case 'fil':
  121. case 'fr':
  122. case 'gun':
  123. case 'hi':
  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 >= 3) && ($number <= 10)) ? 3 : ((($number >= 11) && ($number <= 99)) ? 4 : 5))));
  161. default:
  162. return 0;
  163. }
  164. }
  165. /**
  166. * Overrides the default plural rule for a given locale.
  167. *
  168. * @param string $rule A PHP callable
  169. * @param string $locale The locale
  170. *
  171. * @return null
  172. *
  173. * @throws \LogicException
  174. */
  175. public static function set($rule, $locale)
  176. {
  177. if ("pt_BR" == $locale) {
  178. // temporary set a locale for brazilian
  179. $locale = "xbr";
  180. }
  181. if (strlen($locale) > 3) {
  182. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  183. }
  184. if (!is_callable($rule)) {
  185. throw new \LogicException('The given rule can not be called');
  186. }
  187. self::$rules[$locale] = $rule;
  188. }
  189. // @codeCoverageIgnoreEnd
  190. }