我在cakephp 2.9中有一个多语言的web应用程序与它的.po文件很好地工作但是现在我还需要转换日期和时间字符串,我知道这些字符串由setlocale函数完成。如果我在视图文件中包含setlocale inline但是当我把它放在我的语言更改函数中时(我一直用它来切换语言)它不起作用。
我的语言控制器如下:
<?php
App::uses('AppController', 'Controller');
class IdiomasController extends AppController {
var $uses = array();
public function idioma_spa($u=null)
{
$this->Session->write('Config.language', 'spa');
setlocale(LC_TIME, array('es_ES.UTF-8', 'esp'));
$this->redirect($this->referer());
}
public function idioma_eng($u=null)
{
$this->Session->write('Config.language', 'eng');
setlocale(LC_TIME, array('en_US.UTF-8', 'eng'));
$this->redirect($this->referer());
}
public function idioma_ita($u=null)
{
$this->Session->write('Config.language', 'ita');
setlocale(LC_TIME, array('it_IT.UTF-8', 'ita'));
$this->redirect($this->referer());
}
}
这些函数在导航栏的下拉列表中以这种方式调用:
<?php if($this->Session->read('Config.language')=='spa'):?>
<li class="nav-item dropdown navbar-right">
<a class="nav-link dropdown-toggle" href="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">ES</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="<?php echo $this->Html->url( array('plugin' => null,'controller'=>'idiomas','action'=>'idioma_eng',$url3)); ?>">EN</a>
<a class="dropdown-item" href="<?php echo $this->Html->url( array('plugin' => null,'controller'=>'idiomas','action'=>'idioma_ita',$url3)); ?>">IT</a>
</div>
</li>
<?php elseif($this->Session->read('Config.language')=='eng'):?>
<li class="nav-item dropdown navbar-right">
<a class="nav-link dropdown-toggle" href="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">EN</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="<?php echo $this->Html->url( array('plugin' => null,'controller'=>'idiomas','action'=>'idioma_spa',$url3)); ?>">ES</a>
<a class="dropdown-item" href="<?php echo $this->Html->url( array('plugin' => null,'controller'=>'idiomas','action'=>'idioma_ita',$url3)); ?>">IT</a>
</div>
</li>
<?php elseif($this->Session->read('Config.language')=='ita'):?>
<li class="nav-item dropdown navbar-right">
<a class="nav-link dropdown-toggle" href="" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">IT</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="<?php echo $this->Html->url( array('plugin' => null,'controller'=>'idiomas','action'=>'idioma_eng',$url3)); ?>">EN</a>
<a class="dropdown-item" href="<?php echo $this->Html->url( array('plugin' => null,'controller'=>'idiomas','action'=>'idioma_spa',$url3)); ?>">ES</a>
</div>
</li>
<?php endif;?>
很好地解决了这个问题:
class AppController扩展Controller {public $ components = array('Session');
public function beforeFilter() {
if ($this->Session->check('Config.language')) {
Configure::write('Config.language', $this->Session->read('Config.language'));
if ($this->Session->read('Config.language')=='spa')
setlocale(LC_TIME, array('es_ES.UTF-8', 'esp'));
else if ($this->Session->read('Config.language')=='ita')
setlocale(LC_TIME, array('it_IT.UTF-8', 'ita'));
else
setlocale(LC_TIME, array('en_US.UTF-8', 'eng'));
}
}
}
我的语言控制器就像:
<?php
App::uses('AppController', 'Controller');
class IdiomasController extends AppController {
var $uses = array();
public function idioma_spa($u=null)
{
$this->Session->write('Config.language', 'spa');
$this->redirect($this->referer());
}
public function idioma_eng($u=null)
{
$this->Session->write('Config.language', 'eng');
$this->redirect($this->referer());
}
public function idioma_ita($u=null)
{
$this->Session->write('Config.language', 'ita');
$this->redirect($this->referer());
}
}
不知道它是否是最有效的方式,但却像魅力一样!