首页 > 解决方案 > Cakephp 中的设置语言环境

问题描述

我在 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;?>

标签: phplocalecakephp-2.0

解决方案


很好地解决了这个问题:

类 AppController 扩展控制器 { 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());
    }
}

不知道这是否是最有效的方式,但就像魅力一样!


推荐阅读