Cakephp 3.0.0-RC2 I18n::locale() doesn't works

丶灬走出姿态 提交于 2019-12-11 13:15:59

问题


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


回答1:


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;
        }


来源:https://stackoverflow.com/questions/28847597/cakephp-3-0-0-rc2-i18nlocale-doesnt-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!