开发者

cakephp choosing language by url - part 2

Ok, so I have now working localization based on URL (http://sta开发者_如何学Gockoverflow.com/questions/4550670/cakephp-choosing-language-by-url), but how to create a link that will change the language? I've tried

<?php echo $html->link('English', array('language'=>'eng')); ?>

but all it does is that it adds language:eng at the end of the URL (www.example.com/blah/blah/language:eng instead of www.example.com/eng/blah/blah) and does not switch the language at all.

The routes now look like this:

Router::connect('/registered/:language/:controller',  
                array('prefix' => 'registered', 'registered' => true, 'layout'=> 'registered'),
                array('language' => '[a-z]{3}'));
Router::connect('/registered/:language/:controller/:action/*', 
                array('prefix' => 'registered', 'registered' => true, 'layout'=> 'registered'),
                array('language' => '[a-z]{3}'));

I've also overriden the url method in app_helper:

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

How to set the link to work the way it should be? Thanks


Try removing :language from your router urls.

Not tested, but it should work as your urls already getting named param :language

Router::connect('/registered/:controller',  
                array('prefix' => 'registered', 'registered' => true, 'layout'=> 'registered'),
                array('language' => '[a-z]{3}'));
Router::connect('/registered/:controller/:action/*', 
                array('prefix' => 'registered', 'registered' => true, 'layout'=> 'registered'),
                array('language' => '[a-z]{3}'));

Keep url override method to ensure you accept both type of GET variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜