mardi 4 août 2015

CakePHP 3.0:attempt to create Multilanguage Site

I have a trouble: I'm trying to create multilingual site. I want that all "French" pages have prefix "fre" and all "English" pages have no prefix at all.

but I have one trouble:

When I switch language to "fre" all links would be changed. But if I follow pass through one of new links I'll get page which consists links without prefix "fre"(except "Delete" current Item)

<?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 for help!




Aucun commentaire:

Enregistrer un commentaire