我有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();