cakephpinternationalizationcakephp-2.6cakephp-2.x

Cakephp 2.6 internationalization


I was doing translation for my website using i18n and translate behavior.

Once the user click on the change language button. All the text and records will be shown in Chinese.

But,

when user click on other pages, only the text translated through i18n still displaying in chinese. The database records are showing back to original which is english.

this is the code in the AppController

function beforeFilter() {
    $this->_setLanguage();
}

private function _setLanguage() {

//if the cookie was previously set, and Config.language has not been set
//write the Config.language with the value from the Cookie
    if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
        $this->Session->write('Config.language', $this->Cookie->read('lang'));
    } 

//if the session was previously set, and cookie language has not been set
//write the cookie language with the value from the session

    else if (!$this->Cookie->read('lang') && $this->Session->check('Config.language')) {
        $this->Cookie->write('lang', $this->Session->read('Config.language'));
    }
    //if the user clicked the language URL

    if ( isset($this->params['language']) ) { 
        //then update the value in Session and the one in Cookie
        $this->Session->write('Config.language', $this->params['language']);
        $this->Cookie->write('lang', $this->params['language'], false, '20 days');
    }
}

I am wondering where did I went wrong?

Can anybody help?

Thanks


Solution

  • I18n::translate() uses the Configure.language session value over the configuration value in case set, but the translate behavior will not, it relies on the configuration value only.

    You do not seem to set the Configure.language configuration value (Configure::write('Config.language', $language)), and thus the translate behavior will use the default value defined in your configuration (in case present), hence no translated content is going to be read.

    See also