duanqian8867 2013-12-09 14:31
浏览 55
已采纳

CakePHP多语言静态页面

I have created a multilingual application in CakePHP 2 I want to include some static content in different languages and I'm having trouble setting up the routing.

I have it working for language/controller/action type routes but if I want to have static content how do I route the PagesController to views in View/Pages/membership.ctp for the default language English and View/fr/Pages/abonnement.ctp for the french translation so that the url will be just /membership or /fr/abonnement? And what is the best way to relate the translations to each other so the language switching links will work and reverse routing will work properly?

I have the following routes already

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));

Router::connect('/:language',
                   array('controller' => 'pages', 'action' => 'display', 'home'),
                   array('language' => 'en|fr', 'persist'=>array('language')));

Router::connect('/:language/:controller',
                   array('action' => 'index'),
                   array('language' => 'en|fr', 'persist'=>array('language')));

Router::connect('/:language/:controller/:action/*',
                   array(),
                   array('language' => 'en|fr', 'persist'=>array('language')));

the _setLanguage() method called from AppController's beforeFilter()

protected function _setLanguage() {
    //if the cookie was previously set, and Config.language has not been set
    //write the Config.language with the value from the Cookie
    if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
      $this->Session->write('Config.language', $this->Cookie->read('lang'));
    }
    //if the user clicked the language URL
    else if ( isset($this->params['language']) && ($this->params['language'] !=  $this->Session->read('Config.language'))) {
        // get the correct language code
        $languageCodeEquivalencies = array(
            'fr'=>'fra',
            'en'=>'eng'
        );
        $languageCode = $this->params['language'];
        if(in_array($languageCode, array_keys($languageCodeEquivalencies))) {
            $languageCode = $languageCodeEquivalencies[$languageCode];
        }
      //then update the value in Session and the one in Cookie
      $this->Session->write('Config.language', $languageCode);
      $this->Cookie->write('lang', $languageCode, false, '20 days');
    }
    //ensure that both I18n and TranslateBehavior access the same language value.
    if ($this->Session->check('Config.language')) {
        Configure::write('Config.language', $this->Session->read('Config.language'));
    }
}

and my language switching links look like this

if($this->Session->read('Config.language') == 'fra'):
    echo $this->Html->link('English', array_merge(array('language'=>'en'), $this->passedArgs));
else:
    echo $this->Html->link('Français', array_merge(array('language'=>'fr'), $this->passedArgs));
endif;
  • 写回答

3条回答 默认 最新

  • dongyong3223 2013-12-11 19:18
    关注

    Here's what I added to get it working...

    Controller/AppController.php

    public function beforeFilter() {
      ...
      // render language specific view if it exists
      $locale = Configure::read('Config.language');
      if ($locale && file_exists(APP . 'View' . DS . $locale . DS . $this->viewPath)) {
          // e.g. use /app/View/fra/Pages/tos.ctp instead of /app/View/Pages/tos.ctp
          $this->viewPath = $locale . DS . $this->viewPath;
      }
      ...
    }
    

    Config/routes.php

    Router::connect('/fr/abonnement', array('language'=>'fr', 'controller' => 'pages', 'action' => 'display', 'membership'));
    Router::connect('/en/membership', array('language'=>'en', 'controller' => 'pages', 'action' => 'display', 'membership'));
    

    Which is working fine right now as I have a limited number of static pages and I only need to support two languages. I'm pretty sure manually setting up the routes for each language and static page will become too unwieldy after a decent number of pages. Let me know if you think of a better solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错