我在cakephp2.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;?>发布于 2019-04-18 23:22:44
很好地解决了这个问题:
类会话扩展控制器{ public $components = AppController (‘$components’);
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());
}
}不知道这是不是最有效的方法,但效果很好!
https://stackoverflow.com/questions/55735714
复制相似问题