Zend Framework 菜单翻译
我正在开发一个多语言 Web 应用程序。我有来自 xml 文件的菜单元素,但我不知道如何翻译菜单。另一方面,我可以在视图中翻译网站的正常内容,如下所示:
<?=$this->translate->_('Members Login')
我的引导文件:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
private $_acl = null;
private $_auth = null;
protected function _initAutoLoad()
{
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
if (Zend_Auth::getInstance()->hasIdentity()) {
Zend_Registry::set('role', Zend_Auth::getInstance()->getStorage()->read()->role);
} else {
Zend_Registry::set('role', 'guest');
}
$this->_acl = new Model_LibraryAcl();
$this->_auth = Zend_Auth::getInstance();
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_AccessCheck($this->_acl, $this->_auth));
return $modelLoader;
}
function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('HTML4_STRICT');
$view->headMeta()->appendHttpEquiv('Content-type','text/html;charset=UTF-8')
->appendName('description','The Best Freelance Projects');
$view->headTitle('Freelance Duty');
$navContainerConfig = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml','nav');
$navContainer = new Zend_Navigation($navContainerConfig);
$view->navigation($navContainer)
->setAcl($this->_acl)
->setRole(($this->_auth->getStorage()->read())?$this->_auth->getStorage()->read()->role:'guest')
->findAllBy('class', 'nav_header');
}
function _initSetTranslations()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$translate = new Zend_Translate('gettext',APPLICATION_PATH.'/languages/tr.mo','tr');
$translate->setLocale('tr');
$view->translate = $translate;
Zend_Registry::set('TranslationObject', $translate);
}
}
并编写视图中的菜单如下:
echo $this->navigation()->menu()->setUlClass('nav_header');
我不知道如何翻译菜单 有什么意见吗?
提前致谢
I am developing an multilingual web application.I have menu elemnts which came from xml file but i dont know how to translate menu.On the other hand,I could translate normal content of site in the view like this:
<?=$this->translate->_('Members Login')
My bootstrap file:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
private $_acl = null;
private $_auth = null;
protected function _initAutoLoad()
{
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
if (Zend_Auth::getInstance()->hasIdentity()) {
Zend_Registry::set('role', Zend_Auth::getInstance()->getStorage()->read()->role);
} else {
Zend_Registry::set('role', 'guest');
}
$this->_acl = new Model_LibraryAcl();
$this->_auth = Zend_Auth::getInstance();
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_AccessCheck($this->_acl, $this->_auth));
return $modelLoader;
}
function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('HTML4_STRICT');
$view->headMeta()->appendHttpEquiv('Content-type','text/html;charset=UTF-8')
->appendName('description','The Best Freelance Projects');
$view->headTitle('Freelance Duty');
$navContainerConfig = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml','nav');
$navContainer = new Zend_Navigation($navContainerConfig);
$view->navigation($navContainer)
->setAcl($this->_acl)
->setRole(($this->_auth->getStorage()->read())?$this->_auth->getStorage()->read()->role:'guest')
->findAllBy('class', 'nav_header');
}
function _initSetTranslations()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$translate = new Zend_Translate('gettext',APPLICATION_PATH.'/languages/tr.mo','tr');
$translate->setLocale('tr');
$view->translate = $translate;
Zend_Registry::set('TranslationObject', $translate);
}
}
and write the menu in view like this:
echo $this->navigation()->menu()->setUlClass('nav_header');
i dont know howto translate menu
Any comments?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
Zend_Navigation
时,标题和标签可以自动翻译。查看参考指南。只需选择您的助手并为其设置Zend_Translate
即可。编辑:首先,在评论中的代码中,您做错了两件事:
1. 使用
setUseTranslator
并将翻译器对象传递给其中。但是setUseTranslator
采用布尔值来描述是否使用翻译器。您可以使用setTranslator
方法设置翻译器:2. 在使用链接之前,请确保该方法返回您想要的对象:
所以您不能这样做:
在
上调用
对象,它应该可以工作。setTranslator
$navigationTitles and labels could be translated automatically, when you use
Zend_Navigation
. Check out reference guide. Just pick your helper and setZend_Translate
to it.EDIT: First of all, in code from comment you're doing two things wrong:
1. Using
setUseTranslator
and passing translator object into it. ButsetUseTranslator
takes boolean value describing to use or not translator. You set translator usingsetTranslator
method:2. Before use chaining be sure, that method returns object that you want:
So you can't do:
Call
setTranslator
on$navigation
object and it should work.Zend_Translate 应该有帮助。看一下这里: http://framework.zend.com/manual/en/ zend.translate.html
Zend_Translate should help. Take a look here: http://framework.zend.com/manual/en/zend.translate.html