I have a Cakephp 4 project internationalization used to work but today I tried it changing the PC and it didn't change the language and doesn't give any error.
I have this methid to change language in AppController:
public function changeLang():void
{
$idioma=$this->request->getCookie('idiomacookie');
if (I18n::getLocale() !== 'es_ES'){#No esta en español
I18n::setLocale('es_ES');#Pongo inglés
$idioma = Cookie::create('idiomacookie', 'es_ES');#Cambio cookie
$this->response = $this->response->withCookie($idioma);
$this->idiomacookie = 'es_ES';
}else{
I18n::setLocale('en_US');
$idioma = Cookie::create('idiomacookie', 'en_US');
$this->response = $this->response->withCookie($idioma);
$this->idiomacookie = 'en_US';
}
$this->redirect($this->referer());
}
And in the initialize method I initialize the languge:
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'||$idioma ==NULL){
I18n::setLocale('en_US');
}else{
I18n::setLocale('es_ES');
}
I used var_dumps and it showed that I18n::getLocale() change when I click on the button to change the language but the messages don't change.
I tried the defaultlocale from es_ES to en_US and the messages also didn't change. I have the translation folders like this:
/app
/resources
/locale
/en
default.po
cake.pot
default.pot
The messages always show in spanish (es_ES). I also tried to generate the folders again with :
bin/cake i18n extract
But that also didn't work.
Can someone tell me what I am doing wrong? Please
Try:
// AppController::changeLang()
and AppController::beforeFilter()