CakePHP动态更改区域设置
问题描述:
我的CakePHP应用程序已通过.po
文件国际化。CakePHP动态更改区域设置
文件结构是如下所示:
- 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());
}
}
看到http://stackoverflow.com/questions/28847597/cakephp-3-0-0-rc2-i18nlocale-doesnt-works –
“_doesn't WORK_” 是不是一个适当的问题描述。请具体说明究竟发生了什么情况,并添加适当的代码上下文。 – ndm
问题是,当我在控制器上使用'I18n :: locale('en_EN');'时,只会翻译此控制器中的区域设置,而不是在所有应用程序中。我的想法是在导航栏上放置一个按钮或链接,让您在推送时翻译区域设置。 – Wildchild