4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation
;
15 * Returns the plural rules for a given locale.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class PluralizationRules
21 // @codeCoverageIgnoreStart
22 private static $rules = array();
25 * Returns the plural position to use for the given locale and number.
27 * @param integer $number The number
28 * @param string $locale The locale
30 * @return integer The plural position
32 public static function get($number, $locale)
34 if ("pt_BR" == $locale) {
35 // temporary set a locale for brazilian
39 if (strlen($locale) > 3) {
40 $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
43 if (isset(self
::$rules[$locale])) {
44 $return = call_user_func(self
::$rules[$locale], $number);
46 if (!is_int($return) || $return < 0) {
54 * The plural rules are derived from code of the Zend Framework (2010-09-25),
55 * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd).
56 * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
127 return ($number == 1) ? 0 : 1;
141 return (($number == 0) || ($number == 1)) ? 0 : 1;
149 return (($number %
10 == 1) && ($number %
100 != 11)) ? 0 : ((($number %
10 >= 2) && ($number %
10 <= 4) && (($number %
100 < 10) || ($number %
100 >= 20))) ? 1 : 2);
153 return ($number == 1) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2);
156 return ($number == 1) ? 0 : (($number == 2) ? 1 : 2);
159 return (($number %
10 == 1) && ($number %
100 != 11)) ? 0 : ((($number %
10 >= 2) && (($number %
100 < 10) || ($number %
100 >= 20))) ? 1 : 2);
162 return ($number %
100 == 1) ? 0 : (($number %
100 == 2) ? 1 : ((($number %
100 == 3) || ($number %
100 == 4)) ? 2 : 3));
165 return ($number %
10 == 1) ? 0 : 1;
168 return ($number == 1) ? 0 : ((($number == 0) || (($number %
100 > 1) && ($number %
100 < 11))) ? 1 : ((($number %
100 > 10) && ($number %
100 < 20)) ? 2 : 3));
171 return ($number == 0) ? 0 : ((($number %
10 == 1) && ($number %
100 != 11)) ? 1 : 2);
174 return ($number == 1) ? 0 : ((($number %
10 >= 2) && ($number %
10 <= 4) && (($number %
100 < 12) || ($number %
100 > 14))) ? 1 : 2);
177 return ($number == 1) ? 0 : (($number == 2) ? 1 : ((($number == 8) || ($number == 11)) ? 2 : 3));
180 return ($number == 1) ? 0 : ((($number == 0) || (($number %
100 > 0) && ($number %
100 < 20))) ? 1 : 2);
183 return ($number == 0) ? 0 : (($number == 1) ? 1 : (($number == 2) ? 2 : ((($number >= 3) && ($number <= 10)) ? 3 : ((($number >= 11) && ($number <= 99)) ? 4 : 5))));
191 * Overrides the default plural rule for a given locale.
193 * @param string $rule A PHP callable
194 * @param string $locale The locale
198 * @throws \LogicException
200 public static function set($rule, $locale)
202 if ("pt_BR" == $locale) {
203 // temporary set a locale for brazilian
207 if (strlen($locale) > 3) {
208 $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
211 if (!is_callable($rule)) {
212 throw new \
LogicException('The given rule can not be called');
215 self
::$rules[$locale] = $rule;
218 // @codeCoverageIgnoreEnd