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

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_View
0017  * @subpackage Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Locale */
0024 // require_once 'Zend/Locale.php';
0025 
0026 /** Zend_View_Helper_Abstract.php */
0027 // require_once 'Zend/View/Helper/Abstract.php';
0028 
0029 /**
0030  * Translation view helper
0031  *
0032  * @category  Zend
0033  * @package   Zend_View
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license   http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
0038 {
0039     /**
0040      * Translation object
0041      *
0042      * @var Zend_Translate_Adapter
0043      */
0044     protected $_translator;
0045 
0046     /**
0047      * Constructor for manually handling
0048      *
0049      * @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
0050      */
0051     public function __construct($translate = null)
0052     {
0053         if ($translate !== null) {
0054             $this->setTranslator($translate);
0055         }
0056     }
0057 
0058     /**
0059      * Translate a message
0060      * You can give multiple params or an array of params.
0061      * If you want to output another locale just set it as last single parameter
0062      * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
0063      * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
0064      *
0065      * @param  string $messageid Id of the message to be translated
0066      * @return string|Zend_View_Helper_Translate Translated message
0067      */
0068     public function translate($messageid = null)
0069     {
0070         if ($messageid === null) {
0071             return $this;
0072         }
0073 
0074         $translate = $this->getTranslator();
0075         $options   = func_get_args();
0076 
0077         array_shift($options);
0078         $count  = count($options);
0079         $locale = null;
0080         if ($count > 0) {
0081             if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
0082                 $locale = array_pop($options);
0083             }
0084         }
0085 
0086         if ((count($options) === 1) and (is_array($options[0]) === true)) {
0087             $options = $options[0];
0088         }
0089 
0090         if ($translate !== null) {
0091             $messageid = $translate->translate($messageid, $locale);
0092         }
0093 
0094         if (count($options) === 0) {
0095             return $messageid;
0096         }
0097 
0098         return vsprintf($messageid, $options);
0099     }
0100 
0101     /**
0102      * Sets a translation Adapter for translation
0103      *
0104      * @param  Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
0105      * @throws Zend_View_Exception When no or a false instance was set
0106      * @return Zend_View_Helper_Translate
0107      */
0108     public function setTranslator($translate)
0109     {
0110         if ($translate instanceof Zend_Translate_Adapter) {
0111             $this->_translator = $translate;
0112         } else if ($translate instanceof Zend_Translate) {
0113             $this->_translator = $translate->getAdapter();
0114         } else {
0115             // require_once 'Zend/View/Exception.php';
0116             $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
0117             $e->setView($this->view);
0118             throw $e;
0119         }
0120 
0121         return $this;
0122     }
0123 
0124     /**
0125      * Retrieve translation object
0126      *
0127      * @return Zend_Translate_Adapter|null
0128      */
0129     public function getTranslator()
0130     {
0131         if ($this->_translator === null) {
0132             // require_once 'Zend/Registry.php';
0133             if (Zend_Registry::isRegistered('Zend_Translate')) {
0134                 $this->setTranslator(Zend_Registry::get('Zend_Translate'));
0135             }
0136         }
0137 
0138         return $this->_translator;
0139     }
0140 
0141     /**
0142      * Set's an new locale for all further translations
0143      *
0144      * @param  string|Zend_Locale $locale New locale to set
0145      * @throws Zend_View_Exception When no Zend_Translate instance was set
0146      * @return Zend_View_Helper_Translate
0147      */
0148     public function setLocale($locale = null)
0149     {
0150         $translate = $this->getTranslator();
0151         if ($translate === null) {
0152             // require_once 'Zend/View/Exception.php';
0153             $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
0154             $e->setView($this->view);
0155             throw $e;
0156         }
0157 
0158         $translate->setLocale($locale);
0159         return $this;
0160     }
0161 
0162     /**
0163      * Returns the set locale for translations
0164      *
0165      * @throws Zend_View_Exception When no Zend_Translate instance was set
0166      * @return string|Zend_Locale
0167      */
0168     public function getLocale()
0169     {
0170         $translate = $this->getTranslator();
0171         if ($translate === null) {
0172             // require_once 'Zend/View/Exception.php';
0173             $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
0174             $e->setView($this->view);
0175             throw $e;
0176         }
0177 
0178         return $translate->getLocale();
0179     }
0180 }