0

我的 CakePHP 应用程序通过.po文件国际化了。

文件结构如下:

- src
   - Locale
      - en_EN
      - en_ES

该应用程序在启动时通过以下方式正确翻译:

ini_set('intl.default_locale', 'en_ES');

但是,我需要动态翻译应用程序,例如在动作侦听器按钮中。

我尝试了以下方法,但它不起作用:

use Cake\I18n\I18n;
I18n::locale('en_EN');
4

1 回答 1

1

您需要将语言环境保存在会话中,以便它在页面请求之间保持不变。

一种可能的方法:

class AppController extends Controller {    

    public function initialize() {

        if ($this->request->session()->check('Config.locale')) {    
            I18n::locale($this->request->session()->read('Config.locale'));
        }

        //rest of your init code
    }


    public function change_locale($locale){

        $this->request->session()->write('Config.locale', $locale);
        return $this->redirect($this->referer());
    }    

}
于 2016-01-20T22:07:11.143 回答