首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cakephp中的setlocale

Cakephp中的setlocale
EN

Stack Overflow用户
提问于 2019-04-18 04:44:56
回答 1查看 279关注 0票数 0

我在cakephp2.9中有一个多语言的web应用程序,可以很好地处理它的.po文件,但现在我还需要翻译日期和时间字符串,我知道这是由setlocale函数完成的。如果我将setlocale内联包含在视图文件中,那么当我将它放入我的语言更改函数(我一直使用它来切换语言)中时,它就不起作用了。

我的语言控制器如下所示:

代码语言:javascript
复制
<?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());
    }
}

这些函数在导航栏的下拉列表中以如下方式调用:

代码语言:javascript
复制
            <?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;?>
EN

回答 1

Stack Overflow用户

发布于 2019-04-18 23:22:44

很好地解决了这个问题:

类会话扩展控制器{ public $components = AppController (‘$components’);

代码语言:javascript
复制
    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'));
        }
    }

}

我的语言控制器简单地保持如下所示:

代码语言:javascript
复制
<?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());
    }
}

不知道这是不是最有效的方法,但效果很好!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55735714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档