Cakephp 3.0.0-RC2 I18n :: locale()不工作 [英] Cakephp 3.0.0-RC2 I18n::locale() doesn't works
问题描述
我使用Cakephp 3.0.0-RC2。
工作很好,但是我不能在登录时更改用户的du语言。
我的登录功能不工作。它什么都不做:
public function login()
{
if($ this-> request - > is('post')){
$ user = $ this-> Auth-> identify();
if($ user){
$ this-> Auth-> setUser($ user);
I18n :: locale($ user ['lang']);
return $ this-> redirect($ this-> Auth-> redirectUrl());
}
$ this-> Flash->错误(__(Nom d'utilisateur ou mot de passe incorrect,essayezànouveau。
}
}
请注意,当我更改bootstrap中的语言时,ie ini_set('intl.default_locale','fr_FR');它工作正常,但我想更改语言,当用户登录。
我的桌面运行Wampserver与PHP 5.5.2在Windows 8.1
我检查gettext函数独立的Cakephp。 Il只有在 https://launchpad.net/php-gettext/ 下载后才能正常工作。 p>
你能帮我吗?
Christian
因为您重定向到另一个控制器。哪个平均区域设置可以在该控制器或AppController中重写。为什么不使用会话?
public function login()
{
if - > request-> is('post')){
$ user = $ this-> Auth-> identify();
if($ user){
$ this-> Auth-> setUser($ user);
$ this-> request-> session() - > write('Config.language','du');
return $ this-> redirect($ this-> Auth-> redirectUrl());
}
$ this-> Flash->错误(__(Nom d'utilisateur ou mot de passe incorrect,essayezànouveau。
}
}
在您的AppController(FrontController)
类AppController extends Controller
{
public function initialize()
{
$ this-> loadComponent('Flash');
$ this-> layout ='mysite';
$ lang = $ this-> request-> session() - > read('Config.language');
switch($ lang)
{
casedu:
I18n :: locale('du_DU');
break;
caseen:
I18n :: locale('en_EN');
break;
casejp:
I18n :: locale('ja_JP');
break;
默认值:
I18n :: locale('en_EN');
break;
}
I am using Cakephp 3.0.0-RC2. It work's fine but I can't change du language of the user at login.
My login function doesn't work. It do nothing :
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
I18n::locale($user['lang']);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__("Nom d'utilisateur ou mot de passe incorrect, essayez à nouveau."));
}
}
Notice that when I change the language in bootstrap, ie ini_set('intl.default_locale', 'fr_FR'); it works fine but I want to change the language when the user logged. My desktop is runing Wampserver with PHP 5.5.2 on Windows 8.1 I check the gettext function independantely of Cakephp. Il works but only after having download on https://launchpad.net/php-gettext/
Can you help me ? Christian
Because you redirect to another controller. Which mean locale can be override in that controller or AppController. Why don't you use session?
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->request->session()->write('Config.language', 'du');
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__("Nom d'utilisateur ou mot de passe incorrect, essayez à nouveau."));
}
}
And in your AppController (FrontController)
class AppController extends Controller
{
public function initialize()
{
$this->loadComponent('Flash');
$this->layout = 'mysite';
$lang=$this->request->session()->read('Config.language');
switch($lang)
{
case "du":
I18n::locale('du_DU');
break;
case "en":
I18n::locale('en_EN');
break;
case "jp":
I18n::locale('ja_JP');
break;
default:
I18n::locale('en_EN');
break;
}
这篇关于Cakephp 3.0.0-RC2 I18n :: locale()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!