00001 <?php 00024 require_once 'Zend/Locale.php'; 00025 00027 require_once 'Zend/View/Helper/Abstract.php'; 00028 00037 class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract 00038 { 00044 protected $_translator; 00045 00051 public function __construct($translate = null) 00052 { 00053 if ($translate !== null) { 00054 $this->setTranslator($translate); 00055 } 00056 } 00057 00068 public function translate($messageid = null) 00069 { 00070 if ($messageid === null) { 00071 return $this; 00072 } 00073 00074 $translate = $this->getTranslator(); 00075 if ($translate === null) { 00076 return $messageid; 00077 } 00078 00079 $options = func_get_args(); 00080 array_shift($options); 00081 00082 $count = count($options); 00083 $locale = null; 00084 if ($count > 0) { 00085 if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) { 00086 $locale = array_pop($options); 00087 } 00088 } 00089 00090 if ((count($options) === 1) and (is_array($options[0]) === true)) { 00091 $options = $options[0]; 00092 } 00093 00094 $message = $translate->translate($messageid, $locale); 00095 if (count($options) === 0) { 00096 return $message; 00097 } 00098 00099 return vsprintf($message, $options); 00100 } 00101 00109 public function setTranslator($translate) 00110 { 00111 if ($translate instanceof Zend_Translate_Adapter) { 00112 $this->_translator = $translate; 00113 } else if ($translate instanceof Zend_Translate) { 00114 $this->_translator = $translate->getAdapter(); 00115 } else { 00116 require_once 'Zend/View/Exception.php'; 00117 throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter'); 00118 } 00119 00120 return $this; 00121 } 00122 00131 public function getTranslator() 00132 { 00133 if ($this->_translator === null) { 00134 require_once 'Zend/Registry.php'; 00135 if (Zend_Registry::isRegistered('Zend_Translate')) { 00136 $this->setTranslator(Zend_Registry::get('Zend_Translate')); 00137 } 00138 } 00139 00140 return $this->_translator; 00141 } 00142 00150 public function setLocale($locale = null) 00151 { 00152 $translate = $this->getTranslator(); 00153 if ($translate === null) { 00154 require_once 'Zend/View/Exception.php'; 00155 throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter'); 00156 } 00157 00158 $translate->setLocale($locale); 00159 return $this; 00160 } 00161 00168 public function getLocale() 00169 { 00170 $translate = $this->getTranslator(); 00171 if ($translate === null) { 00172 require_once 'Zend/View/Exception.php'; 00173 throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter'); 00174 } 00175 00176 return $translate->getLocale(); 00177 } 00178 }