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.
18
* @copyright Copyright (c) 2005-2008 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 15766 2009-05-25 20:09:35Z thomas $
24
require_once 'Zend/Locale.php';
26
/** Zend_View_Helper_Abstract.php */
27
require_once 'Zend/View/Helper/Abstract.php';
30
* Translation view helper
34
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
35
* @license http://framework.zend.com/license/new-bsd New BSD License
37
class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
42
* @var Zend_Translate_Adapter
44
protected $_translator;
47
* Constructor for manually handling
49
* @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
51
public function __construct($translate = null)
53
if (empty($translate) === false) {
54
$this->setTranslator($translate);
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);
65
* @param string $messageid Id of the message to be translated
66
* @return string Translated message
68
public function translate($messageid = null)
70
if ($messageid === null) {
74
$translate = $this->getTranslator();
75
if ($translate === null) {
79
$options = func_get_args();
80
array_shift($options);
82
$count = count($options);
85
if (Zend_Locale::isLocale($options[($count - 1)], null, false) !== false) {
86
$locale = array_pop($options);
90
if ((count($options) === 1) and (is_array($options[0]) === true)) {
91
$options = $options[0];
94
$message = $translate->translate($messageid, $locale);
95
if (count($options) === 0) {
99
return vsprintf($message, $options);
103
* Sets a translation Adapter for translation
105
* @param Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
106
* @throws Zend_View_Exception When no or a false instance was set
107
* @return Zend_View_Helper_Translate
109
public function setTranslator($translate)
111
if ($translate instanceof Zend_Translate_Adapter) {
112
$this->_translator = $translate;
113
} else if ($translate instanceof Zend_Translate) {
114
$this->_translator = $translate->getAdapter();
116
require_once 'Zend/View/Exception.php';
117
throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
124
* Retrieve translation object
126
* If none is currently registered, attempts to pull it from the registry
127
* using the key 'Zend_Translate'.
129
* @return Zend_Translate_Adapter|null
131
public function getTranslator()
133
if ($this->_translator === null) {
134
require_once 'Zend/Registry.php';
135
if (Zend_Registry::isRegistered('Zend_Translate') === true) {
136
$this->setTranslator(Zend_Registry::get('Zend_Translate'));
140
return $this->_translator;
144
* Set's an new locale for all further translations
146
* @param string|Zend_Locale $locale New locale to set
147
* @throws Zend_View_Exception When no Zend_Translate instance was set
148
* @return Zend_View_Helper_Translate
150
public function setLocale($locale = null)
152
$translate = $this->getTranslator();
153
if ($translate === null) {
154
require_once 'Zend/View/Exception.php';
155
throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
158
$translate->setLocale($locale);
163
* Returns the set locale for translations
165
* @throws Zend_View_Exception When no Zend_Translate instance was set
166
* @return string|Zend_Locale
168
public function getLocale()
170
$translate = $this->getTranslator();
171
if ($translate === null) {
172
require_once 'Zend/View/Exception.php';
173
throw new Zend_View_Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter');
176
return $translate->getLocale();