8
* This source file is subject to the new BSD license that is bundled
9
* with this package in the file LICENSE.txt.
10
* It is also available through the world-wide-web at this URL:
11
* http://framework.zend.com/license/new-bsd
12
* If you did not receive a copy of the license and are unable to
13
* obtain it through the world-wide-web, please send an email
14
* to license@zend.com so we can send you a copy immediately.
19
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
20
* @version $Id: Translate.php 8420 2008-02-26 16:53:53Z darby $
21
* @license http://framework.zend.com/license/new-bsd New BSD License
25
require_once 'Zend/Locale.php';
28
* Translation view helper
34
class Zend_View_Helper_Translate
38
* @var Zend_Translate_Adapter
40
protected $_translator;
43
* Constructor for manually handling
45
* @param Zend_Translate|Zend_Translate_Adapter $translate
47
public function __construct($translate = null)
49
if (!empty($translate)) {
50
$this->setTranslator($translate);
56
* You can give multiple params or an array of params.
57
* If you want to output another locale just set it as last single parameter
58
* Example 1: translate('%1\$s + %2\$s', $value1, $value2, $locale);
59
* Example 2: translate('%1\$s + %2\$s', array($value1, $value2), $locale);
61
* @param string $messageid
62
* @return string Translated message
64
public function translate($messageid = null)
66
if (null === $messageid) {
70
if (null === ($translate = $this->getTranslator())) {
74
$options = func_get_args();
75
array_shift($options);
77
$count = count($options);
80
if (Zend_Locale::isLocale($options[$count - 1])) {
81
$locale = array_pop($options);
84
if ((count($options) == 1) and (is_array($options[0]))) {
85
$options = $options[0];
87
$message = $translate->translate($messageid, $locale);
88
return vsprintf($message, $options);
92
* Sets a translation Adapter for translation
94
* @param Zend_Translate|Zend_Translate_Adapter $translate
95
* @return Zend_View_Helper_Translate
97
public function setTranslator($translate)
99
if ($translate instanceof Zend_Translate_Adapter) {
100
$this->_translator = $translate;
101
} elseif ($translate instanceof Zend_Translate) {
102
$this->_translator = $translate->getAdapter();
104
require_once 'Zend/View/Exception.php';
105
throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
111
* Retrieve translation object
113
* If none is currently registered, attempts to pull it from the registry
114
* using the key 'Zend_Translate'.
116
* @return Zend_Translate_Adapter|null
118
public function getTranslator()
120
if (null === $this->_translator) {
121
require_once 'Zend/Registry.php';
122
if (Zend_Registry::isRegistered('Zend_Translate')) {
123
$this->setTranslator(Zend_Registry::get('Zend_Translate'));
126
return $this->_translator;
130
* Set's an new locale for all further translations
132
* @param string|Zend_Locale $locale
133
* @return Zend_View_Helper_Translate
135
public function setLocale($locale = null)
137
if (null === ($translate = $this->getTranslator())) {
138
require_once 'Zend/View/Exception.php';
139
throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
141
$translate->setLocale($locale);
146
* Returns the set locale for translations
148
* @return string|Zend_Locale
150
public function getLocale()
152
if (null === ($translate = $this->getTranslator())) {
153
require_once 'Zend/View/Exception.php';
154
throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
156
return $translate->getLocale();