2011-02-12 169 views
1

我正在開發一個多語種的web應用程序。我有來自xml文件的菜單元素,但我不知道如何翻譯菜單。另一方面,我可以像這樣在視圖中翻譯網站的正常內容:Zend框架菜單翻譯

<?=$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

標題和標籤可以自動翻譯,當你使用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對象,它應該工作。

0

Zend_Translate應該有所幫助。看看這裏:http://framework.zend.com/manual/en/zend.translate.html

+0

正如我在我的引導文件中寫道,我有alrady使用zend_translate。問題是關於菜單翻譯。因爲菜單內容不是常量值,它動態來自xml – 2011-02-12 22:44:50

+0

這不重要,該菜單項來自XML。您將PARSED文件傳遞給構造函數`Zend_Navigation`,所以它應該能夠獲取菜單標籤並將其轉換。 – singles 2011-02-12 22:50:33