Zend Framework 菜单翻译

发布于 2024-10-17 06:59:42 字数 2494 浏览 0 评论 0原文

我正在开发一个多语言 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花海 2024-10-24 06:59:42

当您使用 Zend_Navigation 时,标题和标签可以自动翻译。查看参考指南。只需选择您的助手并为其设置 Zend_Translate 即可。

编辑:首先,在评论中的代码中,您做错了两件事:
1. 使用setUseTranslator并将翻译器对象传递给其中。但是setUseTranslator采用布尔值来描述是否使用翻译器。您可以使用 setTranslator 方法设置翻译器:

$view->navigation($navContainer) 
     //RB setUseTranslator is unnecessary, because it's true by deafult
     ->setTranslator(Zend_Registry::get('TranslationObject')); 

2. 在使用链接之前,请确保该方法返回您想要的对象:

$view->navigation($navContainer)
     ->setAcl($this->_acl)
     ->setRole(($this->_auth->getStorage()->read())?$this->_auth->getStorage()->read()->role:'guest')
     ->findAllBy('class', 'nav_header'); //that returns an array, not Zend_Navigation object! 

所以您不能这样做:

$navigation->findAllBy('class', 'nav_header') 
           ->setUseTranslator(Zend_Registry::get('TranslationObject')); //wrong methid here, should be setTranslator, as mentioned above

上调用 setTranslator $navigation 对象,它应该可以工作。

Titles and labels could be translated automatically, when you use Zend_Navigation. Check out reference guide. Just pick your helper and set Zend_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. But setUseTranslator takes boolean value describing to use or not translator. You set translator using setTranslator method:

$view->navigation($navContainer) 
     //RB setUseTranslator is unnecessary, because it's true by deafult
     ->setTranslator(Zend_Registry::get('TranslationObject')); 

2. Before use chaining be sure, that method returns object that you want:

$view->navigation($navContainer)
     ->setAcl($this->_acl)
     ->setRole(($this->_auth->getStorage()->read())?$this->_auth->getStorage()->read()->role:'guest')
     ->findAllBy('class', 'nav_header'); //that returns an array, not Zend_Navigation object! 

So you can't do:

$navigation->findAllBy('class', 'nav_header') 
           ->setUseTranslator(Zend_Registry::get('TranslationObject')); //wrong methid here, should be setTranslator, as mentioned above

Call setTranslator on $navigation object and it should work.

梦里兽 2024-10-24 06:59:42

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文