File indexing completed on 2024-04-28 06:00:03

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_Translate
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  * @see Zend_Loader
0024  */
0025 // require_once 'Zend/Loader.php';
0026 
0027 /**
0028  * @see Zend_Translate_Adapter
0029  */
0030 // require_once 'Zend/Translate/Adapter.php';
0031 
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_Translate
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_Translate {
0040     /**
0041      * Adapter names constants
0042      */
0043     const AN_ARRAY   = 'Array';
0044     const AN_CSV     = 'Csv';
0045     const AN_GETTEXT = 'Gettext';
0046     const AN_INI     = 'Ini';
0047     const AN_QT      = 'Qt';
0048     const AN_TBX     = 'Tbx';
0049     const AN_TMX     = 'Tmx';
0050     const AN_XLIFF   = 'Xliff';
0051     const AN_XMLTM   = 'XmlTm';
0052 
0053     const LOCALE_DIRECTORY = 'directory';
0054     const LOCALE_FILENAME  = 'filename';
0055 
0056     /**
0057      * Adapter
0058      *
0059      * @var Zend_Translate_Adapter
0060      */
0061     private $_adapter;
0062 
0063     /**
0064      * Generates the standard translation object
0065      *
0066      * @param  array|Zend_Config|Zend_Translate_Adapter $options Options to use
0067      * @param  string|array [$content] Path to content, or content itself
0068      * @param  string|Zend_Locale [$locale]
0069      * @throws Zend_Translate_Exception
0070      */
0071     public function __construct($options = array())
0072     {
0073         if ($options instanceof Zend_Config) {
0074             $options = $options->toArray();
0075         } else if (func_num_args() > 1) {
0076             $args               = func_get_args();
0077             $options            = array();
0078             $options['adapter'] = array_shift($args);
0079             if (!empty($args)) {
0080                 $options['content'] = array_shift($args);
0081             }
0082 
0083             if (!empty($args)) {
0084                 $options['locale'] = array_shift($args);
0085             }
0086 
0087             if (!empty($args)) {
0088                 $opt     = array_shift($args);
0089                 $options = array_merge($opt, $options);
0090             }
0091         } else if (!is_array($options)) {
0092             $options = array('adapter' => $options);
0093         }
0094 
0095         $this->setAdapter($options);
0096     }
0097 
0098     /**
0099      * Sets a new adapter
0100      *
0101      * @param  array|Zend_Config|Zend_Translate_Adapter $options Options to use
0102      * @param  string|array [$content] Path to content, or content itself
0103      * @param  string|Zend_Locale [$locale]
0104      * @throws Zend_Translate_Exception
0105      */
0106     public function setAdapter($options = array())
0107     {
0108         if ($options instanceof Zend_Config) {
0109             $options = $options->toArray();
0110         } else if (func_num_args() > 1) {
0111             $args               = func_get_args();
0112             $options            = array();
0113             $options['adapter'] = array_shift($args);
0114             if (!empty($args)) {
0115                 $options['content'] = array_shift($args);
0116             }
0117 
0118             if (!empty($args)) {
0119                 $options['locale'] = array_shift($args);
0120             }
0121 
0122             if (!empty($args)) {
0123                 $opt     = array_shift($args);
0124                 $options = array_merge($opt, $options);
0125             }
0126         } else if (!is_array($options)) {
0127             $options = array('adapter' => $options);
0128         }
0129 
0130         if (Zend_Loader::isReadable('Zend/Translate/Adapter/' . ucfirst($options['adapter']). '.php')) {
0131             $options['adapter'] = 'Zend_Translate_Adapter_' . ucfirst($options['adapter']);
0132         }
0133 
0134         if (!class_exists($options['adapter'])) {
0135             Zend_Loader::loadClass($options['adapter']);
0136         }
0137 
0138         if (array_key_exists('cache', $options)) {
0139             Zend_Translate_Adapter::setCache($options['cache']);
0140         }
0141 
0142         $adapter = $options['adapter'];
0143         unset($options['adapter']);
0144         $this->_adapter = new $adapter($options);
0145         if (!$this->_adapter instanceof Zend_Translate_Adapter) {
0146             // require_once 'Zend/Translate/Exception.php';
0147             throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
0148         }
0149     }
0150 
0151     /**
0152      * Returns the adapters name and it's options
0153      *
0154      * @return Zend_Translate_Adapter
0155      */
0156     public function getAdapter()
0157     {
0158         return $this->_adapter;
0159     }
0160 
0161     /**
0162      * Returns the set cache
0163      *
0164      * @return Zend_Cache_Core The set cache
0165      */
0166     public static function getCache()
0167     {
0168         return Zend_Translate_Adapter::getCache();
0169     }
0170 
0171     /**
0172      * Sets a cache for all instances of Zend_Translate
0173      *
0174      * @param  Zend_Cache_Core $cache Cache to store to
0175      * @return void
0176      */
0177     public static function setCache(Zend_Cache_Core $cache)
0178     {
0179         Zend_Translate_Adapter::setCache($cache);
0180     }
0181 
0182     /**
0183      * Returns true when a cache is set
0184      *
0185      * @return boolean
0186      */
0187     public static function hasCache()
0188     {
0189         return Zend_Translate_Adapter::hasCache();
0190     }
0191 
0192     /**
0193      * Removes any set cache
0194      *
0195      * @return void
0196      */
0197     public static function removeCache()
0198     {
0199         Zend_Translate_Adapter::removeCache();
0200     }
0201 
0202     /**
0203      * Clears all set cache data
0204      *
0205      * @param string $tag Tag to clear when the default tag name is not used
0206      * @return void
0207      */
0208     public static function clearCache($tag = null)
0209     {
0210         Zend_Translate_Adapter::clearCache($tag);
0211     }
0212 
0213     /**
0214      * Calls all methods from the adapter
0215      */
0216     public function __call($method, array $options)
0217     {
0218         if (method_exists($this->_adapter, $method)) {
0219             return call_user_func_array(array($this->_adapter, $method), $options);
0220         }
0221         // require_once 'Zend/Translate/Exception.php';
0222         throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
0223     }
0224 }