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.