web/enmi/Zend/View/Helper/Translate.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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_View
       
    17  * @subpackage Helper
       
    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 20140 2010-01-08 05:21:04Z thomas $
       
    21  */
       
    22 
       
    23 /** Zend_Locale */
       
    24 require_once 'Zend/Locale.php';
       
    25 
       
    26 /** Zend_View_Helper_Abstract.php */
       
    27 require_once 'Zend/View/Helper/Abstract.php';
       
    28 
       
    29 /**
       
    30  * Translation view helper
       
    31  *
       
    32  * @category  Zend
       
    33  * @package   Zend_View
       
    34  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    35  * @license   http://framework.zend.com/license/new-bsd     New BSD License
       
    36  */
       
    37 class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
       
    38 {
       
    39     /**
       
    40      * Translation object
       
    41      *
       
    42      * @var Zend_Translate_Adapter
       
    43      */
       
    44     protected $_translator;
       
    45 
       
    46     /**
       
    47      * Constructor for manually handling
       
    48      *
       
    49      * @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
       
    50      */
       
    51     public function __construct($translate = null)
       
    52     {
       
    53         if ($translate !== null) {
       
    54             $this->setTranslator($translate);
       
    55         }
       
    56     }
       
    57 
       
    58     /**
       
    59      * Translate a message
       
    60      * You can give multiple params or an array of params.
       
    61      * If you want to output another locale just set it as last single parameter
       
    62      * Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
       
    63      * Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
       
    64      *
       
    65      * @param  string $messageid Id of the message to be translated
       
    66      * @return string|Zend_View_Helper_Translate Translated message
       
    67      */
       
    68     public function translate($messageid = null)
       
    69     {
       
    70         if ($messageid === null) {
       
    71             return $this;
       
    72         }
       
    73 
       
    74         $translate = $this->getTranslator();
       
    75         $options   = func_get_args();
       
    76 
       
    77         array_shift($options);
       
    78         $count  = count($options);
       
    79         $locale = null;
       
    80         if ($count > 0) {
       
    81             if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
       
    82                 $locale = array_pop($options);
       
    83             }
       
    84         }
       
    85 
       
    86         if ((count($options) === 1) and (is_array($options[0]) === true)) {
       
    87             $options = $options[0];
       
    88         }
       
    89 
       
    90         if ($translate !== null) {
       
    91             $messageid = $translate->translate($messageid, $locale);
       
    92         }
       
    93 
       
    94         if (count($options) === 0) {
       
    95             return $messageid;
       
    96         }
       
    97 
       
    98         return vsprintf($messageid, $options);
       
    99     }
       
   100 
       
   101     /**
       
   102      * Sets a translation Adapter for translation
       
   103      *
       
   104      * @param  Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
       
   105      * @throws Zend_View_Exception When no or a false instance was set
       
   106      * @return Zend_View_Helper_Translate
       
   107      */
       
   108     public function setTranslator($translate)
       
   109     {
       
   110         if ($translate instanceof Zend_Translate_Adapter) {
       
   111             $this->_translator = $translate;
       
   112         } else if ($translate instanceof Zend_Translate) {
       
   113             $this->_translator = $translate->getAdapter();
       
   114         } else {
       
   115             require_once 'Zend/View/Exception.php';
       
   116             $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
       
   117             $e->setView($this->view);
       
   118             throw $e;
       
   119         }
       
   120 
       
   121         return $this;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Retrieve translation object
       
   126      *
       
   127      * @return Zend_Translate_Adapter|null
       
   128      */
       
   129     public function getTranslator()
       
   130     {
       
   131         if ($this->_translator === null) {
       
   132             require_once 'Zend/Registry.php';
       
   133             if (Zend_Registry::isRegistered('Zend_Translate')) {
       
   134                 $this->setTranslator(Zend_Registry::get('Zend_Translate'));
       
   135             }
       
   136         }
       
   137 
       
   138         return $this->_translator;
       
   139     }
       
   140 
       
   141     /**
       
   142      * Set's an new locale for all further translations
       
   143      *
       
   144      * @param  string|Zend_Locale $locale New locale to set
       
   145      * @throws Zend_View_Exception When no Zend_Translate instance was set
       
   146      * @return Zend_View_Helper_Translate
       
   147      */
       
   148     public function setLocale($locale = null)
       
   149     {
       
   150         $translate = $this->getTranslator();
       
   151         if ($translate === null) {
       
   152             require_once 'Zend/View/Exception.php';
       
   153             $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
       
   154             $e->setView($this->view);
       
   155             throw $e;
       
   156         }
       
   157 
       
   158         $translate->setLocale($locale);
       
   159         return $this;
       
   160     }
       
   161 
       
   162     /**
       
   163      * Returns the set locale for translations
       
   164      *
       
   165      * @throws Zend_View_Exception When no Zend_Translate instance was set
       
   166      * @return string|Zend_Locale
       
   167      */
       
   168     public function getLocale()
       
   169     {
       
   170         $translate = $this->getTranslator();
       
   171         if ($translate === null) {
       
   172             require_once 'Zend/View/Exception.php';
       
   173             $e = new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
       
   174             $e->setView($this->view);
       
   175             throw $e;
       
   176         }
       
   177 
       
   178         return $translate->getLocale();
       
   179     }
       
   180 }