web/lib/Zend/Translate.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Translate
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Translate.php 22591 2010-07-16 20:58:05Z thomas $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Loader
       
    24  */
       
    25 require_once 'Zend/Loader.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Translate_Adapter
       
    29  */
       
    30 require_once 'Zend/Translate/Adapter.php';
       
    31 
       
    32 
       
    33 /**
       
    34  * @category   Zend
       
    35  * @package    Zend_Translate
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_Translate {
       
    40     /**
       
    41      * Adapter names constants
       
    42      */
       
    43     const AN_ARRAY   = 'Array';
       
    44     const AN_CSV     = 'Csv';
       
    45     const AN_GETTEXT = 'Gettext';
       
    46     const AN_INI     = 'Ini';
       
    47     const AN_QT      = 'Qt';
       
    48     const AN_TBX     = 'Tbx';
       
    49     const AN_TMX     = 'Tmx';
       
    50     const AN_XLIFF   = 'Xliff';
       
    51     const AN_XMLTM   = 'XmlTm';
       
    52 
       
    53     const LOCALE_DIRECTORY = 'directory';
       
    54     const LOCALE_FILENAME  = 'filename';
       
    55 
       
    56     /**
       
    57      * Adapter
       
    58      *
       
    59      * @var Zend_Translate_Adapter
       
    60      */
       
    61     private $_adapter;
       
    62 
       
    63     /**
       
    64      * Generates the standard translation object
       
    65      *
       
    66      * @param  array|Zend_Config $options Options to use
       
    67      * @throws Zend_Translate_Exception
       
    68      */
       
    69     public function __construct($options = array())
       
    70     {
       
    71         if ($options instanceof Zend_Config) {
       
    72             $options = $options->toArray();
       
    73         } else if (func_num_args() > 1) {
       
    74             $args               = func_get_args();
       
    75             $options            = array();
       
    76             $options['adapter'] = array_shift($args);
       
    77             if (!empty($args)) {
       
    78                 $options['content'] = array_shift($args);
       
    79             }
       
    80 
       
    81             if (!empty($args)) {
       
    82                 $options['locale'] = array_shift($args);
       
    83             }
       
    84 
       
    85             if (!empty($args)) {
       
    86                 $opt     = array_shift($args);
       
    87                 $options = array_merge($opt, $options);
       
    88             }
       
    89         } else if (!is_array($options)) {
       
    90             $options = array('adapter' => $options);
       
    91         }
       
    92 
       
    93         $this->setAdapter($options);
       
    94     }
       
    95 
       
    96     /**
       
    97      * Sets a new adapter
       
    98      *
       
    99      * @param  array|Zend_Config $options Options to use
       
   100      * @throws Zend_Translate_Exception
       
   101      */
       
   102     public function setAdapter($options = array())
       
   103     {
       
   104         if ($options instanceof Zend_Config) {
       
   105             $options = $options->toArray();
       
   106         } else if (func_num_args() > 1) {
       
   107             $args               = func_get_args();
       
   108             $options            = array();
       
   109             $options['adapter'] = array_shift($args);
       
   110             if (!empty($args)) {
       
   111                 $options['content'] = array_shift($args);
       
   112             }
       
   113 
       
   114             if (!empty($args)) {
       
   115                 $options['locale'] = array_shift($args);
       
   116             }
       
   117 
       
   118             if (!empty($args)) {
       
   119                 $opt     = array_shift($args);
       
   120                 $options = array_merge($opt, $options);
       
   121             }
       
   122         } else if (!is_array($options)) {
       
   123             $options = array('adapter' => $options);
       
   124         }
       
   125 
       
   126         if (Zend_Loader::isReadable('Zend/Translate/Adapter/' . ucfirst($options['adapter']). '.php')) {
       
   127             $options['adapter'] = 'Zend_Translate_Adapter_' . ucfirst($options['adapter']);
       
   128         }
       
   129 
       
   130         if (!class_exists($options['adapter'])) {
       
   131             Zend_Loader::loadClass($options['adapter']);
       
   132         }
       
   133 
       
   134         if (array_key_exists('cache', $options)) {
       
   135             Zend_Translate_Adapter::setCache($options['cache']);
       
   136         }
       
   137 
       
   138         $adapter = $options['adapter'];
       
   139         unset($options['adapter']);
       
   140         $this->_adapter = new $adapter($options);
       
   141         if (!$this->_adapter instanceof Zend_Translate_Adapter) {
       
   142             require_once 'Zend/Translate/Exception.php';
       
   143             throw new Zend_Translate_Exception("Adapter " . $adapter . " does not extend Zend_Translate_Adapter");
       
   144         }
       
   145     }
       
   146 
       
   147     /**
       
   148      * Returns the adapters name and it's options
       
   149      *
       
   150      * @return Zend_Translate_Adapter
       
   151      */
       
   152     public function getAdapter()
       
   153     {
       
   154         return $this->_adapter;
       
   155     }
       
   156 
       
   157     /**
       
   158      * Returns the set cache
       
   159      *
       
   160      * @return Zend_Cache_Core The set cache
       
   161      */
       
   162     public static function getCache()
       
   163     {
       
   164         return Zend_Translate_Adapter::getCache();
       
   165     }
       
   166 
       
   167     /**
       
   168      * Sets a cache for all instances of Zend_Translate
       
   169      *
       
   170      * @param  Zend_Cache_Core $cache Cache to store to
       
   171      * @return void
       
   172      */
       
   173     public static function setCache(Zend_Cache_Core $cache)
       
   174     {
       
   175         Zend_Translate_Adapter::setCache($cache);
       
   176     }
       
   177 
       
   178     /**
       
   179      * Returns true when a cache is set
       
   180      *
       
   181      * @return boolean
       
   182      */
       
   183     public static function hasCache()
       
   184     {
       
   185         return Zend_Translate_Adapter::hasCache();
       
   186     }
       
   187 
       
   188     /**
       
   189      * Removes any set cache
       
   190      *
       
   191      * @return void
       
   192      */
       
   193     public static function removeCache()
       
   194     {
       
   195         Zend_Translate_Adapter::removeCache();
       
   196     }
       
   197 
       
   198     /**
       
   199      * Clears all set cache data
       
   200      *
       
   201      * @param string $tag Tag to clear when the default tag name is not used
       
   202      * @return void
       
   203      */
       
   204     public static function clearCache($tag = null)
       
   205     {
       
   206         Zend_Translate_Adapter::clearCache($tag);
       
   207     }
       
   208 
       
   209     /**
       
   210      * Calls all methods from the adapter
       
   211      */
       
   212     public function __call($method, array $options)
       
   213     {
       
   214         if (method_exists($this->_adapter, $method)) {
       
   215             return call_user_func_array(array($this->_adapter, $method), $options);
       
   216         }
       
   217         require_once 'Zend/Translate/Exception.php';
       
   218         throw new Zend_Translate_Exception("Unknown method '" . $method . "' called!");
       
   219     }
       
   220 }