2015-09-27 119 views
-1

如何創建一個允許用戶使用cakephp 3.1切換網站語言的鏈接?Cakephp 3語言切換器鏈接

我已經創建了一個多語言網站,但我創建了一個鏈接來切換語言,但沒有運氣。

+0

這可能會有幫助:[國際化](https://github.com/dereuromark/cakephp-tools/blob /master/docs/I18n/I18n.md) – mark

回答

0

這裏的指令如何在運行時改變區域設置:http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#changing-the-locale-at-runtime

知道你 - 改變語言可以創建一個動作(即在AppController中),並保存在cookie數據,例如你可以做(​​簡單的例子)在你的AppController:

public function beforeFilter(Event $event) 
{ 
    parent::beforeFilter($event); 

    $lang = $this->Cookie->read('lang'); 

    if (empty($lang)) { 
     return; 
    } 

    I18n::locale($lang); 
} 

public function changeLang($lang = 'en_US') 
{ 
    $this->Cookie->write('lang', $lang); 
    return $this->redirect($this->request->referer()); 
} 
在你看來

然後:

<?= $this->Html->link('Change language to PL', ['action' => 'changeLang', 'pl_PL']); ?>