0

I'm trying to save the runtime language an offer users the option of switch it.

I have this in my default.php:

          <li class="nav-item">
            <?php 
            if (I18n::getLocale() !== 'en_US'){ 
          ?>              
              <?= $this->Html->link('EN', ['action' => 'changeLang']); ?>
              <?php
            }else{ ?>
              <?= $this->Html->link('ES', ['action' => 'ccc']); ?>
            <?php    } ?>
          </li>              

I have never worked with Cookies so I'm a bit lost in this part. In my AppController I have this in the method initialize:

public function initialize(): void
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Authentication.Authentication');
    $idioma=$this->request->getCookie($idiomacookie);
    if ($idioma == 'en_US'){
        I18n::setLocale('es_ES');

    }else{
        I18n::setLocale('en_US');
    }
}

And o the method changLAng I have this:

public function changeLang():void
{
    if (I18n::getLocale() !== 'en_US'){
        I18n::setLocale('en_US');
        $idioma = Cookie::create('idiomacookie', 'en_US');
        $this->response = $this->response->withCookie($idioma);
        $this->idiomacookie = 'en_US';
    }else{
        I18n::setLocale('es_ES');
        $idioma = Cookie::create('idiomacookie', 'es_ES');
        $this->response = $this->response->withCookie($idioma);
        $this->idiomacookie = 'es_ES';
    }

    $this->redirect($this->referer());

}

Where do I declare the Cookie so it doesn't always take the default value? I'm not sure if I'm using $this->response->withCookie correctly.

Iria
  • 9
  • 4
  • You have never declared `$idiomacookie`, so it's `null`, and that function needs a string with the name of the cookie to read. This error message is pretty self-explanatory. – Greg Schmidt Jan 09 '23 at 16:57
  • 1
    When you try to set it, you're creating a Cookie object in one instance, but doing nothing with it. Cookies need to be added to the response in order to be of any use. In the other instance, you're doing `$this->request->('idiomacookie',['value'=>'en_US']);`, which is just nonsensical. – Greg Schmidt Jan 09 '23 at 16:58
  • That's why I ask where I should declare /inicialize the Cookie. Because I think that if I declare it in initialize it will always take the value I add. – Iria Jan 09 '23 at 17:31
  • @greg-schmidt How can I add the cookie then? – Iria Jan 09 '23 at 17:32
  • Have you read the linked duplicate question? It's got quite a thorough answer. – Greg Schmidt Jan 09 '23 at 21:21
  • You almost certainly want `getCookie('idiomacookie')`, not `getCookie($idiomacookie)`. Setting `$this->idiomacookie` is not going to do anything at all. And why would you set your locale to `es` in your `initialize` function when the cookie says that the user has selected `en`? – Greg Schmidt Jan 09 '23 at 21:26
  • I did read the duplicate, but I can't write on the cookie value. I do this (as the duplicate say): $idioma = Cookie::create('idiomacookie', 'es_ES'); $this->response = $this->response->withCookie($idioma); $this->idiomacookie = 'es_ES'; But the cookie 'idiomacookie' value doesn't change. – Iria Jan 10 '23 at 14:51
  • I put in spanish when the cookie say en because for some reason I can't change the name of default (it should be spanish but is english). If in app.php I put es_ES and call de locales folder en, it doesn't work. – Iria Jan 10 '23 at 14:55
  • I changed it again and it worked – Iria Jan 10 '23 at 15:05

0 Answers0