CakePHP 2.1 URL 语言参数 [英] CakePHP 2.1 URL Language Parameter

查看:18
本文介绍了CakePHP 2.1 URL 语言参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的多语言 CakePHP 2.1 应用程序终于几乎可以运行了.

I finally have my multilingual CakePHP 2.1 app almost working.

首先,我在core.php的末尾定义了默认语言:

First, I define the default language at the end of core.php:

/* Define default language */
Configure::write('Config.language', 'eng');

这是我在 AppControler.php 中的代码:

Here is my code in AppControler.php:

public function beforeFilter() {
parent::beforeFilter();
    $this->_setLanguage();
    //Configure::write('Config.language', 'fre'); //Manually change the language to test .po file
    $this->Auth->allow('index','view','home','display','logout');
}

function _setLanguage() {

    if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
        $this->Session->write('Config.language', $this->Cookie->read('lang'));
    }
    else if (isset($this->params['language']) && ($this->params['language']
             !=  $this->Session->read('Config.language'))) {

        $this->Session->write('Config.language', $this->params['language']);
        $this->Cookie->write('lang', $this->params['language'], false, '20 days');
    }
}

如果我在 AppController.php 中取消注释 Configure::write('Config.language', 'fre');,则整个站点都是法语(除了数据库驱动的内容,我计划为此使用 TranslateBehavior).但是,我想在单击按钮时使用基于 URL 的语言切换,这就是应用程序崩溃的地方.这是我的路线,基于这个 详细教程:

If I uncomment Configure::write('Config.language', 'fre'); in AppController.php, the whole site is in French (except database driven content, for which I plan to use TranslateBehavior). However, I want to use URL-based language switching on a button click, and that's where the app breaks down. Here is my route, based on this nuts-and-bolts tutorial:

Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{2}'));

这是我的按钮代码:

<?php echo $this->Html->link($this->Html->image('../img/PUZ013-US-Flag.png', array('border' => '0')),array('language'=>'eng'),array('target' => '_parent', 'escape' => false));;?>&nbsp <?php echo $this->Html->link($this->Html->image('../img/PUZ013-FR-Flag.png', array('border' => '0')),array('language'=>'fre'),array('target' => '_parent', 'escape' => false));?>

我在 AppHelper 中有这个来处理 URL 切换,但它不起作用.URL 应该是 example.com/fre/controller/action,但它是 example.com/controller/action/language:fre,并且 cookie 没有改变.

I have this in AppHelper to handle the URL switching, but it's not working. The URL should be example.com/fre/controller/action, but instead it is example.com/controller/action/language:fre, and the cookie is not changing.

class AppHelper extends Helper {

  public function url($url = null, $full = false) {
        if(!isset($url['language']) && isset($this->params['language'])) {
          $url['language'] = $this->params['language'];
        }

        return parent::url($url, $full);
   }

}

如果您单击标志,则什么也不会发生.我错过了什么?

If you click on the flags, nothing happens. What am I missing?

推荐答案

我最终将一个函数添加到我的 AppController 并在我的 beforeFilter() 中调用它:

I ended up adding a function to my AppController and calling it in my beforeFilter():

我的 beforeFilter() 看起来像这样:

My beforeFilter() looks like this:

public function beforeFilter() {
    parent::beforeFilter();
    $this->_checkRoute();
    $this->Auth->allow('index','view','home','display','logout');
}

这是_checkRoute()函数:

function _checkRoute() {
        $params = $this->params['pass'];
        $url = $this->here;

        if (strpos($url, 'language:fre')) {
            $this->Session->write('Config.language', 'fre'); 
            Configure::write('Config.language', 'fre');
        }

        elseif (strpos($url, 'language:eng')) {
            Configure::write('Config.language', 'eng');
            $this->Session->write('Config.language', 'eng');
        }

}

当用户单击语言图标按钮时,将翻译单个页面.唯一剩下的问题是当用户单击菜单链接时会话会取消设置,因为 URL 参数没有被维护.另一天的另一个谜题.

When a user clicks the language icon button, the individual page is translated. The only remaining issue is that the session unsets when the user clicks on a menu link, as the URL parameter is not maintained. Another puzzle for another day.

这篇关于CakePHP 2.1 URL 语言参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆