Christian BERTAUD
Christian BERTAUD

Reputation: 13

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

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

Upvotes: 1

Views: 1199

Answers (1)

Giang Nguyễn
Giang Nguyễn

Reputation: 46

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

Upvotes: 2

Related Questions