CakePHP网站的语言切换

时间:2017-05-19 08:23:36

标签: cakephp

我试图在CakePHP网站上进行语言切换,但我似乎无法将正确的信息传递给我的函数。

在我的网站上,我有两个英语和荷兰语按钮:

<?php echo $this->Html->link(__('ENG'), array('controller' => 'Webpages', 'action' => 'checkLanguage', 'ENG'), array('class' => 'pure-button button-small')); ?>
<?php echo $this->Html->link(__('NL'), array('controller' => 'Webpages', 'action' => 'checkLanguage', 'NL'), array('class' => 'pure-button button-small')); ?>

这两个按钮将'ENG'或'NL'字符串传递给我的webpagesController.php中的函数,此时字符串应该在参数中给出的$ selectlanguage中。

public function checkLanguage($selectlanguage){
    $this -> loadModel('Language');

    //get information from $selectlanguage
    $selectedlanguage = $this -> Session -> read('thisSelectedLanguage');

    //set information for $selectedlanguage
    $this -> Session -> write('thisSelectedLanguage', $selectedlanguage);

    //set language ID   
    $this -> Language -> id = $languageID;

   //get language id
    $thisLanguage = $this -> Language -> find('first', array(
        'conditions' => array(
            'Language.language_id' => $languageID,
            'Language.language_code' => $selectedlanguage
        )
    ));

    // expose data-record to the related view
    $this -> set('thisLanguage', $thisLanguage);


    $this -> redirect(array(
        'controller' => 'Webpages',
        'action' => 'homepage'
    ));

} 

在这个函数中,我试图将正确的ID(Language.language_id)与包含“ENG”或“NL”的变量$ selectlanguage相匹配。

'NL'和'ENG'是我在语言表中存储的两个ISO代码(Language.language_code)。

如果我在变量中有正确的ID,我需要能够将其传递给与页面对应的函数。

webpagesController.php中的主页功能:

public function homepage() {

        // load the website base-layout
        $this -> layout = 'homepagelayout';
        // load relevant models
        $this -> loadModel('Image');
        $this -> loadModel('Newstext');
        $this -> loadModel('Contenttext');        

        $activelanguage = 2;

        $mda = $this -> Contenttext -> find('first', array(
            'conditions' => array(
                'Contentitem.contentitem_slug' => 'mda', 
                'Language.language_id' => $activelanguage
            )
        ));
        $this->set('mda', $mda);
}

在$ activelanguage之后,'2'需要替换为包含与所选语言对应的正确ID的变量。

有人能帮助我吗?我似乎接近解决方案。

2 个答案:

答案 0 :(得分:0)

请检查 $ languageID 是否未定义。您必须将 $ languageID 替换为 $ selectlanguag e,或将 $ selectlanguage 分配给 $ languageID

答案 1 :(得分:0)

我的问题是,我写错了函数。

public function checkLanguage($selectlanguage){

//set session key
$this -> Session -> write('thisSelectedLanguage', $selectlanguage); 


    $this -> redirect(array(
        'controller' => 'Webpages',
        'action' => 'homepage'
    ));

}

上述功能只需设置会话密钥,其余信息需要放在相应的网页功能中。

public function homepage() {

    // load the website base-layout
    $this -> layout = 'homepagelayout';
    // load relevant models
    $this -> loadModel('Image');
    $this -> loadModel('Newstext');
    $this -> loadModel('Contenttext');
    $this -> loadModel('Language');        

    //get ISO code 
    $selectedlanguage = $this -> Session -> read('thisSelectedLanguage');

    //get language id
    $thisLanguage = $this -> Language -> find('first', array(
        'conditions' => array(
            'Language.language_code' => $selectedlanguage
        )
    ));

    // expose data-record to the related view
    $this -> set('thisLanguage', $thisLanguage);


    // set $activelanguage to current language.
    $activelanguage = $thisLanguage['Language']['language_id'];
    // send snippets to screen

    $mda = $this -> Contenttext -> find('first', array(
        'conditions' => array(
            'Contentitem.contentitem_slug' => 'mda', 
            'Language.language_id' => $activelanguage
        )
    ));
    $this->set('mda', $mda);