Zend Framework菜单翻译

时间:2011-02-12 22:35:53

标签: php zend-framework navigation

我正在开发一个多语言网络应用程序。我有来自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');

我不知道如何翻译菜单 有什么意见吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

使用Zend_Navigation时,标题和标签可以自动翻译。查看reference guide。只需选择你的助手并将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,它应该可以正常工作。

答案 1 :(得分:0)

Zend_Translate应该有所帮助。看看这里:http://framework.zend.com/manual/en/zend.translate.html