我在cakephp 2.9中有一个多语言的Web应用程序,其.po文件和其他文件都可以很好地工作,但现在我还需要翻译日期和时间字符串,我知道这些字符串是由setlocale函数完成的。如果我在视图文件中包含setlocale内联,则可以正常工作,但是当我将其放入我的语言更改功能(自从一直以来,我一直使用它来切换语言)时,它就无法工作。
我的语言控制器如下:
<?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;?>
答案 0 :(得分:0)
很好地解决了这个问题:
AppController类扩展了Controller { 公共$ 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());
}
}
不知道它是否是最有效的方法,但是却像魅力一样!