File indexing completed on 2024-12-22 05:37:11

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Locale
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * Utility class for returning the plural rules according to the given locale
0024  *
0025  * @category   Zend
0026  * @package    Zend_Locale
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 class Zend_Translate_Plural
0031 {
0032     /**
0033      * Manual rule to use
0034      *
0035      * @var string
0036      */
0037     protected static $_plural = array();
0038 
0039     /**
0040      * Returns the plural definition to use
0041      *
0042      * @param  integer $number Number for plural selection
0043      * @param  string  $locale Locale to use
0044      * @return integer Plural number to use
0045      */
0046     public static function getPlural($number, $locale)
0047     {
0048         if ($locale == "pt_BR") {
0049             // temporary set a locale for brasilian
0050             $locale = "xbr";
0051         }
0052 
0053         if (strlen($locale) > 3) {
0054             $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
0055         }
0056 
0057         if (isset(self::$_plural[$locale])) {
0058             $return = call_user_func(self::$_plural[$locale], $number);
0059 
0060             if (!is_int($return) || ($return < 0)) {
0061                 $return = 0;
0062             }
0063 
0064             return $return;
0065         }
0066 
0067         switch($locale) {
0068             case 'az':
0069             case 'bo':
0070             case 'dz':
0071             case 'id':
0072             case 'ja':
0073             case 'jv':
0074             case 'ka':
0075             case 'km':
0076             case 'kn':
0077             case 'ko':
0078             case 'ms':
0079             case 'th':
0080             case 'tr':
0081             case 'vi':
0082             case 'zh':
0083                 return 0;
0084                 break;
0085 
0086             case 'af':
0087             case 'bn':
0088             case 'bg':
0089             case 'ca':
0090             case 'da':
0091             case 'de':
0092             case 'el':
0093             case 'en':
0094             case 'eo':
0095             case 'es':
0096             case 'et':
0097             case 'eu':
0098             case 'fa':
0099             case 'fi':
0100             case 'fo':
0101             case 'fur':
0102             case 'fy':
0103             case 'gl':
0104             case 'gu':
0105             case 'ha':
0106             case 'he':
0107             case 'hu':
0108             case 'is':
0109             case 'it':
0110             case 'ku':
0111             case 'lb':
0112             case 'ml':
0113             case 'mn':
0114             case 'mr':
0115             case 'nah':
0116             case 'nb':
0117             case 'ne':
0118             case 'nl':
0119             case 'nn':
0120             case 'no':
0121             case 'om':
0122             case 'or':
0123             case 'pa':
0124             case 'pap':
0125             case 'ps':
0126             case 'pt':
0127             case 'so':
0128             case 'sq':
0129             case 'sv':
0130             case 'sw':
0131             case 'ta':
0132             case 'te':
0133             case 'tk':
0134             case 'ur':
0135             case 'zu':
0136                 return ($number == 1) ? 0 : 1;
0137 
0138             case 'am':
0139             case 'bh':
0140             case 'fil':
0141             case 'fr':
0142             case 'gun':
0143             case 'hi':
0144             case 'ln':
0145             case 'mg':
0146             case 'nso':
0147             case 'xbr':
0148             case 'ti':
0149             case 'wa':
0150                 return (($number == 0) || ($number == 1)) ? 0 : 1;
0151 
0152             case 'be':
0153             case 'bs':
0154             case 'hr':
0155             case 'ru':
0156             case 'sr':
0157             case 'uk':
0158                 return (($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
0159 
0160             case 'cs':
0161             case 'sk':
0162                 return ($number == 1) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2);
0163 
0164             case 'ga':
0165                 return ($number == 1) ? 0 : (($number == 2) ? 1 : 2);
0166 
0167             case 'lt':
0168                 return (($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2);
0169 
0170             case 'sl':
0171                 return ($number % 100 == 1) ? 0 : (($number % 100 == 2) ? 1 : ((($number % 100 == 3) || ($number % 100 == 4)) ? 2 : 3));
0172 
0173             case 'mk':
0174                 return ($number % 10 == 1) ? 0 : 1;
0175 
0176             case 'mt':
0177                 return ($number == 1) ? 0 : ((($number == 0) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3));
0178 
0179             case 'lv':
0180                 return ($number == 0) ? 0 : ((($number % 10 == 1) && ($number % 100 != 11)) ? 1 : 2);
0181 
0182             case 'pl':
0183                 return ($number == 1) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2);
0184 
0185             case 'cy':
0186                 return ($number == 1) ? 0 : (($number == 2) ? 1 : ((($number == 8) || ($number == 11)) ? 2 : 3));
0187 
0188             case 'ro':
0189                 return ($number == 1) ? 0 : ((($number == 0) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2);
0190 
0191             case 'ar':
0192                 return ($number == 0) ? 0 : (($number == 1) ? 1 : (($number == 2) ? 2 : ((($number >= 3) && ($number <= 10)) ? 3 : ((($number >= 11) && ($number <= 99)) ? 4 : 5))));
0193 
0194             default:
0195                 return 0;
0196         }
0197     }
0198 
0199     /**
0200      * Set's a new plural rule
0201      *
0202      * @param string $rule   Callback which acts as rule
0203      * @param string $locale Locale which is used for this callback
0204      * @return null
0205      */
0206     public static function setPlural($rule, $locale)
0207     {
0208         if ($locale == "pt_BR") {
0209             // temporary set a locale for brasilian
0210             $locale = "xbr";
0211         }
0212 
0213         if (strlen($locale) > 3) {
0214             $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
0215         }
0216 
0217         if (!is_callable($rule)) {
0218             // require_once 'Zend/Translate/Exception.php';
0219             throw new Zend_Translate_Exception('The given rule can not be called');
0220         }
0221 
0222         self::$_plural[$locale] = $rule;
0223     }
0224 }