CakePHP 动态更改语言环境
CakePHP change locale dynamically
我的 CakePHP 应用程序已通过 .po
个文件国际化。
文件结构如下:
- src
- Locale
- en_EN
- en_ES
应用程序在启动时由以下人员正确翻译:
ini_set('intl.default_locale', 'en_ES');
但是,我需要动态翻译应用程序,例如在动作侦听器按钮中。
我尝试了以下方法,但它不起作用:
use Cake\I18n\I18n;
I18n::locale('en_EN');
您需要在会话中保存区域设置,以便它在页面请求之间持续存在。
可能的方法:
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());
}
}
我的 CakePHP 应用程序已通过 .po
个文件国际化。
文件结构如下:
- src
- Locale
- en_EN
- en_ES
应用程序在启动时由以下人员正确翻译:
ini_set('intl.default_locale', 'en_ES');
但是,我需要动态翻译应用程序,例如在动作侦听器按钮中。
我尝试了以下方法,但它不起作用:
use Cake\I18n\I18n;
I18n::locale('en_EN');
您需要在会话中保存区域设置,以便它在页面请求之间持续存在。
可能的方法:
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());
}
}