Cakephp I18n setLocale doesn't change language

77 Views Asked by At

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

1

There are 1 best solutions below

0
Salines On

Try:

// AppController::changeLang()

public function changeLang($language)
{
    $redirect = $this->getRequest()->getQuery('redirect');

    if (in_array($language, ['es_ES', 'en_US'])) {
        $expiry = new FrozenTime('+ 1 year');
        $this->setResponse($this->getResponse()->withCookie(new Cookie('setLanguage', $language, $expiry, null, null, true, true, 'Lax')));
    }



    return $this->redirect($redirect);
}

and AppController::beforeFilter()

public function beforeFilter(EventInterface $event)
{
    parent::beforeFilter($event);

    $this->Authentication->addUnauthenticatedActions([
        'changeLang',
        ]);

    $acceptLanguage = $this->getRequest()->getHeaderLine('Accept-Language');
    $getCookie = $this->getRequest()->getCookie('setLanguage');
    $acceptCookie = in_array($getCookie, ['es_ES', 'en_US']);
    $setLanguage = $acceptCookie ? $getCookie : (substr($acceptLanguage, 0, 2) == 'es' ? 'es_ES' : 'en_US');
    I18n::setLocale($setLanguage);

}