Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cakephp动态更改语言_Cakephp_Multilingual_Cakephp 2.1_Cakephp Model - Fatal编程技术网

cakephp动态更改语言

cakephp动态更改语言,cakephp,multilingual,cakephp-2.1,cakephp-model,Cakephp,Multilingual,Cakephp 2.1,Cakephp Model,我的网站有两种语言(英语和意大利语) 我的SiteMessagesController.php控制器代码: public function index() { $this->SiteMessage->locale = 'it'; $this->SiteMessage->recursive = 0; $this->set('siteMessages', $this->paginate()); } 以上代码以意大利语显示所有消息。如果我c

我的网站有两种语言(英语和意大利语)

我的SiteMessagesController.php控制器代码:

public function index() {
    $this->SiteMessage->locale = 'it';
    $this->SiteMessage->recursive = 0;
    $this->set('siteMessages', $this->paginate());
}
以上代码以意大利语显示所有消息。如果我
comment
第一行代码,那么它将以英语显示消息

如果我在整个站点中使用这个,我必须编写
$this->SiteMessage->locale='it'行在
$this->ModelName->find()之前在每个控制器的每个操作中


有没有办法将每个模型的
$locale
设置为
'it'

这是给英语的

public$locale='en_us'

如果你问的是我认为你在问的问题,那么这样做可以:

Configure::write('Config.language', 'ita');

如果您将其放置在类似于
AppController::beforeFilter()
的位置,它应该适用于整个应用程序,而不是单个模型或查询。有关更多信息,请参阅上一本书。

@lakumg:你确定你没有覆盖某个地方吗?我链接的页面明确声明,这一行就是它工作所需的全部。所以你的答案不适用。@lakumg:我知道你在使用translate行为,因此从我链接的页面上可以看到:“这样做将确保I18n和TranslateBehavior访问相同的语言值。”。除非你正在做一些非常特别的事情,并且在某个地方凌驾于某些事情之上,否则这正是应该起作用的。你的问题令人困惑。我猜@Hannibal Lecker博士的答案就是你想要的。