在编辑/查看操作上更改语言后,CakePHP重定向

时间:2015-03-05 10:42:01

标签: cakephp cakephp-2.0 cakephp-2.1 cakephp-2.3

我正在使用多语言在cakephp中构建应用程序,例如当我在应用程序的主页上并且我更改语言时它会改变,但是当我在编辑或查看部分时会出现问题:

example.com/en/item/edit/1

我在编辑视图中将语言从en更改为它,重定向到:

example.com/it/item/edit 

但是它确实保持传递的参数。

我该怎么做才能让它发挥作用????

我用来改变语言的链接是:

<?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);
}

1 个答案:

答案 0 :(得分:2)

将所有语言切换链接更改为与此类似:

$passedArgs = $this->request->params['named'] + $this->request->params['pass'];

$this->Html->link('English', array('language'=>'en') + $passedArgs);