我遇到了麻烦: 我正在尝试创建多语言网站。我希望所有“法语”页面都有前缀“fre”,所有“英文”页面都没有前缀。
但我有一个麻烦:
当我将语言切换为“fre”时,所有链接都将被更改。但是,如果我按照通过其中一个新链接的方式传递,我将获得包含没有前缀“fre”的链接的页面(“删除”当前项目除外)
<?php
class AppController extends Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('Cookie');
$this->loadComponent('Auth', [
'loginRedirect' => [
'controller' => 'Items',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Categories',
'action' => 'index'
]
]);
$this->_init_language();
}
public function beforeFilter(Event $event)
{
$this->_setLanguage();
$this->Auth->allow(['index', 'view', 'display']);
}
public function redirect( $url, $status = NULL, $exit = true ) {
$session = $this->request->session();
if($session->check('Config.language')){
if (is_array($url) && !isset($url['language'])) {
$url['language'] = $session->read('Config.language');
}else{
$url = $session->read('Config.language');
}
}
parent::redirect($url,$status,$exit);
}
private function _init_language(){
$language = !isset($this->request->query['language']) ? $this->request->session()->read('Config.language')
: $this->request->query['language'];
switch($language)
{
case "du":
I18n::locale('du_DU');
break;
case "ru":
I18n::locale('ru_RU');
break;
case "jp":
I18n::locale('ja_JP');
break;
default:
I18n::locale('en_EN');
break;
}
}
private function _setLanguage()
{
$session = $this->request->session();
if ($this->Cookie->read('language') && !$session->check('Config.language')) {
$session->write('Config.language', $this->Cookie->read('language'));
}
else if (isset($this->request->query['language']) && ($this->request->query['language'] != $session->read('Config.language'))) {
$session->write('Config.language', $this->request->query['language']);
$this->Cookie->write('language', $this->request->query['language'], false, '20 days');
}
}
//LanguageHelper
<?php
namespace App\View\Helper;
use Cake\View\Helper\HtmlHelper;
class LanguageHelper extends HtmlHelper {
public function url($url = null, $full = false) {
$session = $this->request->session();
if(isset($this->request->query['language'])){
$language = $this->request->query['language'];
if(is_array($url) && !isset($url['language'])) {
$url['language'] = $language;
} else {
$url = $language;
}
}
return parent::url($url, $full);
}
public function link($title, $url = null, array $options = []){
if(isset($this->request->query['language'])){
$language = $this->request->query['language'];
if(is_array($url) && !isset($url['language'])) {
$url['language'] = $language;
} else{
$url = $language;
}
}
return parent::link($title, $url, $options);
}
//AppView
<?php
namespace App\View;
use Cake\View\View;
class AppView extends View
{
public function initialize()
{
$this->loadHelper('Html', [
'className' => 'Language'
]);
}
}
}
$routes->connect('/:language', ['controller' => 'Categories', 'action' => 'index'],['language' => 'ru|de|en|fr']);
$routes->connect('/:language/:controller/:action/*', [], ['language' => 'ru|de|en|fr','routeClass' => 'InflectedRoute']);
$routes->connect('/:language/:controller', ['action' => 'index'], ['language' => 'ru|de|en|fr','routeClass' => 'InflectedRoute']);
Thax寻求帮助!