2012-11-06 157 views
1

所以這是我在我的引導文件Zend的翻譯+自定義網址

/* 
* Here the Translator is enabled and passed to 
* Zend_Registry so can be accesed from anywhere 
*/ 
public static function translateOption() 
{ 
    Zend_Loader::loadClass('Zend_Translate'); 
    $translate = new Zend_Translate(
      'Zend_Translate_Adapter_Csv', 
      self::$root."/lang/en/language.csv", 
      'en'); 
    self::$registry->translate = $translate; 
} 

我試圖才達到像mysite.com/language/controller/action一個URL

這是我的路線。

;Multilang routes 
lang_default.type        = "iM_Controller_Router_Route_Language" 
lang_default.route    = ":language/:controller/:action/*" 
lang_default.defaults.module  = default 
lang_default.defaults.controller = index 
lang_default.defaults.action  = index 
lang_default.defaults.language = en 
lang_default.reqs.language  = "(ro|en)" 

這裏是路由控制器插件,我發現在互聯網上:

<?php 

/** 
* Front Controller Plugin; Created by Gabriel Solomon (http://www.gsdesign.ro/blog/optimizing-zend-routing/) 
* 
* @uses  Zend_Controller_Plugin_Abstract 
* @category iM 
* @package iM_Controller 
* @subpackage Plugins 
*/ 
class iM_Controller_Plugin_Router extends Zend_Controller_Plugin_Abstract{ 
    protected $_dir; 
    protected $_default = array(); 
    protected $_request; 

    protected $_initialConfig; 
    protected $_remainingConfig; 


    public function routeStartup(Zend_Controller_Request_Abstract $request) 
    { 
     // define some routes (URLs) 
     $router = Zend_Controller_Front::getInstance()->getRouter();  
     $this->setRequest($request);  

     $config = $this->getInitial(); 
     $router->addConfig($config); 
    } 


    public function routeShutdown(Zend_Controller_Request_Abstract $request) 
    { 
     $router = Zend_Controller_Front::getInstance()->getRouter(); 

     $config = $this->getRemaining(); 

     $router->addConfig($config); 
    } 

    public function setDir($dir) { 
     $this->_dir = $dir; 
    } 


    public function setDefault($default) { 
     if (is_array($default)) { 
      $this->_default = array_merge($this->_default, $default); 
     } else { 
      $this->_default[] = $default; 
     } 
    } 


    public function setRequest(Zend_Controller_Request_Abstract $request) { 
     $this->_request = $request; 
     //return $this; 
    } 

    public function getInitial() { 
     if ($this->_initialConfig == null) { 
      $this->parseIniDir(); 
     } 

     return $this->_initialConfig; 

    } 


    public function getRemaining() { 
     if ($this->_remainingConfig == null) { 
      $this->parseIniDir(); 
     } 

     return $this->_remainingConfig; 
    } 

    protected function parseIniDir() { 
     $files = $this->getFiles(); 
     $this->_default; 

     $this->_default[] = $this->determineInitial(); 

     $this->_initialConfig = new Zend_Config(array(), true); 
     $this->_remainingConfig = new Zend_Config(array(), true);  

     if (is_array($files)) { 

      foreach ($files as $file) { 
       $routerFile = $this->compilePath($file); 

       if (in_array($file, $this->_default)) { 
        $this->_initialConfig->merge(new Zend_Config_Ini($routerFile)); 

       } else { 
        $this->_remainingConfig->merge(new Zend_Config_Ini($routerFile)); 
       } 
      } 
     } 
    } 

    protected function getFiles() { 
     if (is_dir($this->_dir)) { 
      $dir = new DirectoryIterator($this->_dir); 
      $files = array(); 
      foreach($dir as $fileInfo) { 
       if(!$fileInfo->isDot() && $fileInfo->isFile()) { 
        $files[] = $fileInfo->__toString(); 
       } 
      } 

      return $files; 
     } 

     return false; 
    } 


    protected function getOtherRoutes() { 
     $routes->merge(new Zend_Config_Ini($routerFile)); 
    } 


    protected function determineInitial() { 
     if ($this->_request) { 
      $uri = $this->_request->getRequestUri(); 
      $base = $this->_request->getBasePath() . '/'; 

      $request = str_replace($base, '', $uri); 
      $requestParts = explode('/', $request); 

      $lang = $requestParts[0]; 
      (array_key_exists(1,$requestParts) ? $section = $requestParts[1] : $section = ''); 

      if (!empty($section) && $section == 'user') { 
       return 'user.ini'; 
      } 

     } 
     return false; 
    } 


    protected function compilePath($file) { 
     return $this->_dir . '/' . $file; 
    } 
} 

現在我的問題是,我該如何改變語言,如果URL/EN或/ RO或/德我想我必須改變它的引導程序功能translateOption但如何,我也想提一下,這也有一些錯誤...但我現在會很高興如果你能幫助我如何改變語言,如果網址更改,謝謝!

我不知道是否有人會讀這...但希望如此

回答

0

在引導過程中,應用程序並不「知道」什麼是你網址暫時還。 Bootstrapping用來準備你的應用程序,然後讓它運行。因此,在引導過程之後,dispatchloop會啓動。URL在路由期間被解析,然後在解析URL時,調度過程將調用與路由中的詳細信息關聯的模塊/控制器/操作。

而不是閱讀和配置你的翻譯引導,我會建議這樣做在控制器插件。在dispatchloop的各個步驟中,每個控制器插件中的某些方法被調用。

這裏有一個例子:

class Application_Model_Language extends Zend_Controller_Plugin_Abstract 
{ 
    public function routeShutdown(\Zend_Controller_Request_Abstract $request) 
    { 
     // ask the language from the $request object 
     // load the translation data 
    } 
} 

然後,所有你需要做的,就是告訴你要一個插件添加到dispatchloop的Zend_Application。這是在引導過程中完成的。所以要麼你把它放在你的通用Bootstrap.php中,要麼放在特定於模塊的Bootstrap.php中:

protected function _initTranslations() 
{ 
    $this->bootstrap('frontController'); 
    $front = $this->getResource('frontController'); 
    $front->registerPlugin(new Application_Model_Language()); 
}