所以这就是我的引导文件中的内容
/*
* 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 或 /de 我想我必须在引导函数中更改它translateOption
但是如何,我还要提一下这也有一些错误。 ..但是我现在会很高兴如果您能帮助我在网址更改时如何更改语言,谢谢!
我不知道是否有人会读这个……但希望如此