假设我有一个控制器,我想定义一些 const 变量来保存一些消息(例如错误消息等)。
有没有办法让它被翻译?下面定义了一个示例类:
<?php
namespace Test\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AccountController extends AbstractActionController
{
protected $testError = 'There was an error while testing some stuff';
public function testAction(){
// I know i can use the following approach but I don't want to
// since I want to use a variable for readability issues.
// $testError = $this->getTranslator()->translate('There was an error..');
return new ViewModel();
}
/**
* Retrieve the translator
*
* @return \Zend\I18n\Translator\Translator
*/
public function getTranslator()
{
if (!$this->translator) {
$this->setTranslator($this->getServiceLocator()->get('translator'));
}
return $this->translator;
}
/**
* Set the translator
*
* @param $translator
*/
public function setTranslator($translator)
{
$this->translator = $translator;
}
}
所以我想翻译 testError 。我知道我可以只使用该消息并通过 zend 翻译器翻译它而不使用变量,但我仍然想将它存储在一个变量中以解决可读性问题。对此有任何帮助或其他方法吗?