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

查看:102
本文介绍了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

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中的write('Config.language','fre'); ,整个网站是法语的(除了数据库驱动的内容,我计划使用TranslateBehavior)。但是,我想在按钮点击时使用基于网址的语言切换,这就是应用崩溃的地方。这里是我的路线,基于这螺母和螺栓教程

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);
   }

}

什么都没发生。

推荐答案

我最后向我的AppController添加了一个函数,并在我的 beforeFilter()

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

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

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

这里是 _checkRoute $ c> function:

Here is the _checkRoute() function:

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');
        }

}

当用户单击语言图标按钮,单个页面被翻译。唯一剩下的问题是,当用户点击菜单链接时,会话将取消设置,因为未保留网址参数。另一个难题是另一天。

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天全站免登陆