CakePHP多语言静态页面

时间:2013-12-09 14:31:25

标签: php cakephp localization cakephp-2.4 cakephp-routing

我在CakePHP 2中创建了一个多语言应用程序我想要包含一些不同语言的静态内容,而我在设置路由时遇到了问题。

我让它适用于language/controller/action类型路由但是如果我想要静态内容,我如何将PagesController路由到View/Pages/membership.ctp中的视图以获取默认语言英语和View/fr/Pages/abonnement.ctp法语翻译,以便网址只是/会员资格或/ fr / abonnement?什么是将翻译相互关联的最佳方式,以便语言切换链接起作用,反向路由将正常工作?

我已经有以下路线

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

从AppController的_setLanguage()

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

我的语言切换链接看起来像这样

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 个答案:

答案 0 :(得分:1)

这是我为了让它正常工作而添加的内容......

控制器/ 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;
  }
  ...
}

配置/ 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'));

现在工作正常,因为我的静态页面数量有限,我只需要支持两种语言。我非常确定手动设置每种语言的路由,静态页面在经过一定数量的页面后会变得过于笨重。如果您想到更好的解决方案,请告诉我。

答案 1 :(得分:0)

最简单的方法是使用CakePHP Themes并拥有"主题"对于每种语言。然后只需在您知道需要使用静态语言特定文件的地方设置主题。

答案 2 :(得分:0)

我有类似的设置。这可能是PagesController中的显示功能:

public function display($page) {
    return $this->render(implode('/', compact($this->request->language, 'page')));
}

这将映射到您期待的文件夹结构。