Cakephp 3语言切换器链接

时间:2015-09-27 16:43:38

标签: cakephp

如何使用cakephp 3.1创建允许用户切换网站语言的链接?

我创建了一个多语言网站,但我用谷歌搜索创建一个切换语言的链接,但没有运气。

1 个答案:

答案 0 :(得分:0)

以下是如何在运行时更改语言环境的说明:http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#changing-the-locale-at-runtime

知道你 - 可以创建一个动作(即在AppController中)来改变语言并将这些数据保存在cookie中,你可以在你的AppController中做到(简单示例):

public static boolean isSubstring(String original, String str){
    int counter = 0, oLength = original.length(), sLength = str.length();
    char[] orgArray = original.toCharArray(), sArray = str.toCharArray();
    for(int i = 0 ; i < oLength; i++){
        System.out.println("counter at start of loop " + counter);
        System.out.println(String.format("comparing %s with %s", orgArray[i], sArray[counter]));
        if(orgArray[i] == sArray[counter]){
            counter++;
            System.out.println("incrementing counter " + counter);
        }else{
            //Special case where the character preceding the i'th character is duplicate
            if(counter > 0){
                i -= counter;
            }
            counter = 0;
            System.out.println("resetting counter " + counter);
        }
        if(counter == sLength){
            return true;
        }
    }
    return false;
}

然后在你看来:

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);

    $lang = $this->Cookie->read('lang');

    if (empty($lang)) {
        return;
    }

    I18n::locale($lang);
}

public function changeLang($lang = 'en_US')
{
    $this->Cookie->write('lang', $lang);
    return $this->redirect($this->request->referer());
}