菜谱项目

PluralizationRules.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. case 'af':
  66. case 'bn':
  67. case 'bg':
  68. case 'ca':
  69. case 'da':
  70. case 'de':
  71. case 'el':
  72. case 'en':
  73. case 'eo':
  74. case 'es':
  75. case 'et':
  76. case 'eu':
  77. case 'fa':
  78. case 'fi':
  79. case 'fo':
  80. case 'fur':
  81. case 'fy':
  82. case 'gl':
  83. case 'gu':
  84. case 'ha':
  85. case 'he':
  86. case 'hu':
  87. case 'is':
  88. case 'it':
  89. case 'ku':
  90. case 'lb':
  91. case 'ml':
  92. case 'mn':
  93. case 'mr':
  94. case 'nah':
  95. case 'nb':
  96. case 'ne':
  97. case 'nl':
  98. case 'nn':
  99. case 'no':
  100. case 'om':
  101. case 'or':
  102. case 'pa':
  103. case 'pap':
  104. case 'ps':
  105. case 'pt':
  106. case 'so':
  107. case 'sq':
  108. case 'sv':
  109. case 'sw':
  110. case 'ta':
  111. case 'te':
  112. case 'tk':
  113. case 'ur':
  114. case 'zu':
  115. return (1 == $number) ? 0 : 1;
  116. case 'am':
  117. case 'bh':
  118. case 'fil':
  119. case 'fr':
  120. case 'gun':
  121. case 'hi':
  122. case 'hy':
  123. case 'ln':
  124. case 'mg':
  125. case 'nso':
  126. case 'xbr':
  127. case 'ti':
  128. case 'wa':
  129. return ((0 == $number) || (1 == $number)) ? 0 : 1;
  130. case 'be':
  131. case 'bs':
  132. case 'hr':
  133. case 'ru':
  134. case 'sr':
  135. case 'uk':
  136. return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
  137. case 'cs':
  138. case 'sk':
  139. return (1 == $number) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2);
  140. case 'ga':
  141. return (1 == $number) ? 0 : ((2 == $number) ? 1 : 2);
  142. case 'lt':
  143. return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
  144. case 'sl':
  145. return (1 == $number % 100) ? 0 : ((2 == $number % 100) ? 1 : (((3 == $number % 100) || (4 == $number % 100)) ? 2 : 3));
  146. case 'mk':
  147. return (1 == $number % 10) ? 0 : 1;
  148. case 'mt':
  149. return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3));
  150. case 'lv':
  151. return (0 == $number) ? 0 : (((1 == $number % 10) && (11 != $number % 100)) ? 1 : 2);
  152. case 'pl':
  153. return (1 == $number) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2);
  154. case 'cy':
  155. return (1 == $number) ? 0 : ((2 == $number) ? 1 : (((8 == $number) || (11 == $number)) ? 2 : 3));
  156. case 'ro':
  157. return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2);
  158. case 'ar':
  159. 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))));
  160. default:
  161. return 0;
  162. }
  163. }
  164. /**
  165. * Overrides the default plural rule for a given locale.
  166. *
  167. * @param callable $rule A PHP callable
  168. * @param string $locale The locale
  169. */
  170. public static function set(callable $rule, $locale)
  171. {
  172. if ('pt_BR' === $locale) {
  173. // temporary set a locale for brazilian
  174. $locale = 'xbr';
  175. }
  176. if (strlen($locale) > 3) {
  177. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  178. }
  179. self::$rules[$locale] = $rule;
  180. }
  181. }