web/Zend/Application/Resource/Translate.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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_Application
       
    17  * @subpackage Resource
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Translate.php 22968 2010-09-18 19:50:02Z intiilapa $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Application_Resource_ResourceAbstract
       
    25  */
       
    26 require_once 'Zend/Application/Resource/ResourceAbstract.php';
       
    27 
       
    28 
       
    29 /**
       
    30  * Resource for setting translation options
       
    31  *
       
    32  * @uses       Zend_Application_Resource_ResourceAbstract
       
    33  * @category   Zend
       
    34  * @package    Zend_Application
       
    35  * @subpackage Resource
       
    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_Application_Resource_Translate extends Zend_Application_Resource_ResourceAbstract
       
    40 {
       
    41     const DEFAULT_REGISTRY_KEY = 'Zend_Translate';
       
    42 
       
    43     /**
       
    44      * @var Zend_Translate
       
    45      */
       
    46     protected $_translate;
       
    47 
       
    48     /**
       
    49      * Defined by Zend_Application_Resource_Resource
       
    50      *
       
    51      * @return Zend_Translate
       
    52      */
       
    53     public function init()
       
    54     {
       
    55         return $this->getTranslate();
       
    56     }
       
    57 
       
    58     /**
       
    59      * Retrieve translate object
       
    60      *
       
    61      * @return Zend_Translate
       
    62      * @throws Zend_Application_Resource_Exception if registry key was used
       
    63      *          already but is no instance of Zend_Translate
       
    64      */
       
    65     public function getTranslate()
       
    66     {
       
    67         if (null === $this->_translate) {
       
    68             $options = $this->getOptions();
       
    69 
       
    70             if (!isset($options['content']) && !isset($options['data'])) {
       
    71                 require_once 'Zend/Application/Resource/Exception.php';
       
    72                 throw new Zend_Application_Resource_Exception('No translation source data provided.');
       
    73             } else if (array_key_exists('content', $options) && array_key_exists('data', $options)) {
       
    74                 require_once 'Zend/Application/Resource/Exception.php';
       
    75                 throw new Zend_Application_Resource_Exception(
       
    76                     'Conflict on translation source data: choose only one key between content and data.'
       
    77                 );
       
    78             }
       
    79 
       
    80             if (empty($options['adapter'])) {
       
    81                 $options['adapter'] = Zend_Translate::AN_ARRAY;
       
    82             }
       
    83 
       
    84             if (!empty($options['data'])) {
       
    85                 $options['content'] = $options['data'];
       
    86                 unset($options['data']);
       
    87             }
       
    88 
       
    89             if (isset($options['options'])) {
       
    90                 foreach($options['options'] as $key => $value) {
       
    91                     $options[$key] = $value;
       
    92                 }
       
    93             }
       
    94 
       
    95             if (!empty($options['cache']) && is_string($options['cache'])) {
       
    96                 $bootstrap = $this->getBootstrap();
       
    97                 if ($bootstrap instanceof Zend_Application_Bootstrap_ResourceBootstrapper &&
       
    98                     $bootstrap->hasPluginResource('CacheManager')
       
    99                 ) {
       
   100                     $cacheManager = $bootstrap->bootstrap('CacheManager')
       
   101                         ->getResource('CacheManager');
       
   102                     if (null !== $cacheManager &&
       
   103                         $cacheManager->hasCache($options['cache'])
       
   104                     ) {
       
   105                         $options['cache'] = $cacheManager->getCache($options['cache']);
       
   106                     }
       
   107                 }
       
   108             }
       
   109 
       
   110             $key = (isset($options['registry_key']) && !is_numeric($options['registry_key']))
       
   111                  ? $options['registry_key']
       
   112                  : self::DEFAULT_REGISTRY_KEY;
       
   113             unset($options['registry_key']);
       
   114 
       
   115             if(Zend_Registry::isRegistered($key)) {
       
   116                 $translate = Zend_Registry::get($key);
       
   117                 if(!$translate instanceof Zend_Translate) {
       
   118                     require_once 'Zend/Application/Resource/Exception.php';
       
   119                     throw new Zend_Application_Resource_Exception($key
       
   120                                    . ' already registered in registry but is '
       
   121                                    . 'no instance of Zend_Translate');
       
   122                 }
       
   123 
       
   124                 $translate->addTranslation($options);
       
   125                 $this->_translate = $translate;
       
   126             } else {
       
   127                 $this->_translate = new Zend_Translate($options);
       
   128                 Zend_Registry::set($key, $this->_translate);
       
   129             }
       
   130         }
       
   131 
       
   132         return $this->_translate;
       
   133     }
       
   134 }