2012-11-06 158 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()); 
}