cakephp 2多语言切换器会跳过第二个参数

时间:2019-03-29 13:33:50

标签: internationalization multilingual cakephp-2.7

我有cakephp2多语言应用程序
它使用了3个字母的语言字符串,但我将3个字母更改为2个字母(eng到en,是ar)。

现在它在切换语言(如
localhost/project1/posts/index工作正常
localhost/project1/posts/add工作正常
localhost/project1/posts/edit/1不起作用
localhost/project1/posts/view/1不起作用
在最后2个链接上切换语言时,它会从网址中跳过1个。表示它正在跳过除控制器和操作以外的任何参数。
我的代码是这样的

MyHtmlHelper

<?php 
App::uses('HtmlHelper', 'View/Helper');
class MyHtmlHelper extends HtmlHelper {
    public function url($url = null, $full = false){ 
        if(!isset($url['language']) && isset($this->params['multilengual'])){
            $url['language'] = $this->params['multilengual'];
        }
        return parent::url($url, $full);
  } 
}

AppController.php

    class AppController extends Controller {
        public $components = array('Cookie','Session');
        public $helpers = array('Html' => array('className' => 'MyHtml'));

        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'])){ 
                if ($this->params['language'] != $this->Session->read('Config.language')){  
                    *****Added this for 2 letter lang string in url*********
                    switch( $this->params['language'] ) {
                        case 'ar':
                            $lang = 'are';
                            break;
                        case 'en':
                        default:
                            $lang = 'eng';
                            break;
                    }
                    *******
                    $this->Session->write('Config.language', $lang);
                    $this->Cookie->write('lang', $lang, false, '20 days');
                } 
            } 
        }
        public function beforeFilter(){ 
            $this->_setLanguage(); 
        }
    }

Routs.php

    Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
    Router::connect('/:language/:controller/:action/*', array(), array('language' => 'en|ar'));
    Router::connect('/:language/:controller', array('action' => 'index'), array('language' => 'en|ar'));
    Router::connect('/:language', array('controller' => 'posts', 'action' => 'index'), array('language' => 'en|ar')) ;


    Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'index'));
    CakePlugin::routes();

0 个答案:

没有答案