php - CakePHP 4 Internationalization not working

I have a controller method, which has this line I18n::setLocale('de_DE');. I also have the following file structure, according to ver.4 documentation.

-resources
--locales
---de_DE
----default.po

The latter file contains a single translation. In short, I do everything exactly like Cake tutorial about internationalization says, but message is not translated and stays in English as written in the code. Is there any additional settings to be made in order to programmatically change interface language?

Answer

Solution:

My cakephp code is:

public function changeLanguage(){

        if($this->request->is('post')){
            $this->autoRender = false;
            $this->set('csrfToken', $this->request->getAttribute('csrfToken'));
            $selected = $this->request->getData('language');
            $this->request->getSession()->write('Config.language', $selected);
            I18n::setLocale('de_DE'); //hardcoded just for testing
            return $this->response->withType('application/json')->withStringBody(json_encode(array('status' => 'OK')));
        }
    }

And frontend is JS driven:



function openLangModal(){

    let token = $('[name="_csrfToken"]').val();

    $("#lang-select-btn").unbind();
    $("#lang-select-btn").click(function(){
        var lang = $("#language").val();
        var postUrl = '<path-to-backend>/changeLanguage';

        $.ajax({

            url: postUrl,
            async: true,
            dataType: 'json',
            type: 'POST',
            data: {language: lang, _csrfToken: token},
            success: function(data){
                location.href=location.href;
            }
        });
    });
    $("#langModal").modal("show");
}

Source