在 edit/view 操作上更改语言后 CakePHP 重定向
CakePHP redirect after changing language on edit/view action
我正在用多语言在 cakephp 中构建应用程序,例如,当我在应用程序的主页上并更改语言时,它会更改正常,但是当我在编辑或查看部分时会出现问题,例如:
example.com/en/item/edit/1
我在重定向到的编辑视图中将语言从 en 更改为它:
example.com/it/item/edit
但它并没有按预期保留传递的参数。
我应该怎么做才能让它发挥作用????
我用来更改语言的 link 是:
<?php
echo "<ul>";
echo "<li>". $this->Html->link('English', array('language'=>'en')) . "</li>";
echo "<li>". $this->Html->link('Italian', array('language'=>'it')). "</li>";
echo "<li>". $this->Html->link('German', array('language'=>'de')). "</li>";
echo "<li>". $this->Html->link('Franch', array('language'=>'fr')). "</li>";
echo "<li>". $this->Html->link('Spanish', array('language'=>'es')). "</li>";
echo "<li>". $this->Html->link('Russian', array('language'=>'ru')). "</li>";
echo "<li>". $this->Html->link('Chinesse', array('language'=>'zh')). "</li>";
echo "<li>". $this->Html->link('Portuguese', array('language'=>'pt')). "</li>";
echo "<ul>";
?>
我使用的路由是:
/*** Languages */
Router::connect('/:language',array('controller' => 'properties', 'action' => 'index','home'),array('language' => '[a-z]{2}'));
Router::connect('/:language/:controller',array('controller' => 'pages','action' => 'index'),array('language' => '[a-z]{2}'));
Router::connect('/:language/:controller/:action/*', array('controller' => 'pages','action' => null), array('language' => '[a-z]{2}'));
而我在 beforeFilter 中调用的 AppController 上的函数是:
private function _setLanguage() {
if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
$this->Session->write('Config.language', $this->Cookie->read('lang'));
} else if (isset($this->params['language']) && ($this->params['language'] != $this->Session->read('Config.language'))){
$this->Session->write('Config.language', $this->params['language']);
$this->Cookie->write('lang', $this->params['language'], false, '2 days');
}
}
此外,我已经在 AppController 上覆盖了重定向方法,如下所示:
public function redirect( $url, $status = NULL, $exit = true ) {
if (!isset($url['language']) && $this->Session->check('Config.language')) {
$url['language'] = $this->Session->read('Config.language');
}
parent::redirect($url,$status,$exit);
}
将您所有的语言切换链接更改为与此类似:
$passedArgs = $this->request->params['named'] + $this->request->params['pass'];
$this->Html->link('English', array('language'=>'en') + $passedArgs);
我正在用多语言在 cakephp 中构建应用程序,例如,当我在应用程序的主页上并更改语言时,它会更改正常,但是当我在编辑或查看部分时会出现问题,例如:
example.com/en/item/edit/1
我在重定向到的编辑视图中将语言从 en 更改为它:
example.com/it/item/edit
但它并没有按预期保留传递的参数。
我应该怎么做才能让它发挥作用????
我用来更改语言的 link 是:
<?php
echo "<ul>";
echo "<li>". $this->Html->link('English', array('language'=>'en')) . "</li>";
echo "<li>". $this->Html->link('Italian', array('language'=>'it')). "</li>";
echo "<li>". $this->Html->link('German', array('language'=>'de')). "</li>";
echo "<li>". $this->Html->link('Franch', array('language'=>'fr')). "</li>";
echo "<li>". $this->Html->link('Spanish', array('language'=>'es')). "</li>";
echo "<li>". $this->Html->link('Russian', array('language'=>'ru')). "</li>";
echo "<li>". $this->Html->link('Chinesse', array('language'=>'zh')). "</li>";
echo "<li>". $this->Html->link('Portuguese', array('language'=>'pt')). "</li>";
echo "<ul>";
?>
我使用的路由是:
/*** Languages */
Router::connect('/:language',array('controller' => 'properties', 'action' => 'index','home'),array('language' => '[a-z]{2}'));
Router::connect('/:language/:controller',array('controller' => 'pages','action' => 'index'),array('language' => '[a-z]{2}'));
Router::connect('/:language/:controller/:action/*', array('controller' => 'pages','action' => null), array('language' => '[a-z]{2}'));
而我在 beforeFilter 中调用的 AppController 上的函数是:
private function _setLanguage() {
if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
$this->Session->write('Config.language', $this->Cookie->read('lang'));
} else if (isset($this->params['language']) && ($this->params['language'] != $this->Session->read('Config.language'))){
$this->Session->write('Config.language', $this->params['language']);
$this->Cookie->write('lang', $this->params['language'], false, '2 days');
}
}
此外,我已经在 AppController 上覆盖了重定向方法,如下所示:
public function redirect( $url, $status = NULL, $exit = true ) {
if (!isset($url['language']) && $this->Session->check('Config.language')) {
$url['language'] = $this->Session->read('Config.language');
}
parent::redirect($url,$status,$exit);
}
将您所有的语言切换链接更改为与此类似:
$passedArgs = $this->request->params['named'] + $this->request->params['pass'];
$this->Html->link('English', array('language'=>'en') + $passedArgs);