Cakephp 3.0.0-RC2 I18n::locale()没有';t有效


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->error(__("Nom d'utilisateur ou mot de passe incorrect, essayez à nouveau."));
    }
}

注意,当我在bootstrap中更改语言时,即ini_set('intl.default_locale','fr_fr');它运行良好,但我想在用户登录时更改语言。我的桌面正在Windows 8.1上运行带有PHP 5.5.2的Wampserver我独立于Cakephp检查gettext函数。Il工作,但只有在下载后https://launchpad.net/php-gettext/

你能帮我吗?Christian

因为您重定向到另一个控制器。这意味着可以在该控制器或AppController中重写区域设置。为什么不使用会话?

    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."));
    }
}

在您的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;
        }
相关文章: