2011-08-27 94 views
0

我正在嘗試爲我的網站添加一點語言更改器。我希望使用cookie保存語言。將語言更改器添加到Cookies

如何修改此代碼以實現此目標?

<script src="https://www.google.com/jsapi?key=ABQIAAAAMt9YZQaG_wbfmb2826e_wBTPUDO5TVHJS-HZdrhJpqnB5yZcfRQFObTd1-bPphEIY11228Z78VhA6A"></script> 
    <script src="http://davidwalsh.name/dw-content/mootools-1.3.js"></script> 
    <script> 
     // Set the original/default language 
     var lang = "en"; 
     var currentClass = "currentLang"; 

     // Load the language lib 
     google.load("language",1); 

     // When the DOM is ready.... 
     window.addEvent("domready",function(){ 
      // Retrieve the DIV to be translated. 
      var translateDiv = document.id("languageBlock"); 
      // Define a function to switch from the currentlanguage to another 
      var callback = function(result) { 
       if(result.translation) { 
        translateDiv.set("html",result.translation); 
       } 
      }; 
      // Add a click listener to update the DIV 
      $$("#languages a").addEvent("click",function(e) { 
       // Stop the event 
       if(e) e.stop(); 
       // Get the "to" language 
       var toLang = this.get("rel"); 
       // Set the translation into motion 
       google.language.translate(translateDiv.get("html"),lang,toLang,callback); 
       // Set the new language 
       lang = toLang; 
       // Add class to current 
       this.getSiblings().removeClass(currentClass); 
       this.addClass(currentClass); 
      }); 
     }); 

    </script> 

<div id="languages"><p> 
     <a href="?lang=en" rel="en">English</a>/<a href="?lang=es" rel="es">Spanish</a> 
    </p></div> 

    <div id="languageBlock"> 
     <p>Lights go out and I can't be saved <br /> 
     Tides that I tried to swim against <br /> 
     Brought me down upon my knees <br /> 
     Oh I beg, I beg and plead </p> 

     <p>Singin', come out if things aren't said <br /> 
     Shoot an apple off my head <br /> 
     And a, trouble that can't be named <br /> 
     Tigers waitin' to be tamed </p> 

     <p>Singing, yooooooooooooo ohhhhhh <br /> 
     Yoooooooooooo ohhhhhh </p> 

     <p>Home, home, where I wanted to go <br /> 
     Home, home, where I wanted to go <br /> 
     Home, home, where I wanted to go <br /> 
     Home, home, where I wanted to go</p> 
    </div> 
+0

您確定要使用Cookie嗎? HTML5的新[localStorage](https://developer.mozilla.org/en/dom/storage#localStorage)將使工作變得更容易。 另外,您正在使用鏈接將它們重定向到翻譯的頁面。如果他們已經在http://foo.bar/?lang=en上,並且他們點擊了西班牙語鏈接,它會將他們帶到http://foo.bar/?lang=en?lang=es儘可能我可以告訴。 –

回答

0

聽起來就像您使用的是MooTools。考慮使用MooTools cookie functionality

將這個大功告成在你的鏈接點擊事件處理程序切換語言時:

// Add class to current 
this.getSiblings().removeClass(currentClass); 
this.addClass(currentClass); 

//now set the cookie 
var myCookie = Cookie.write('userLang', toLang); 

當你想讀取cookie,或者在頁面加載(或任何事件你like):

alert('This user has their language set to: ' + Cookie.read('userLang')); 
+0

那麼,一個問題仍然不能保存實際的語言。 – Shawn31313

+0

你是什麼意思,「不保存」? 'Cookie.write'保存值。它將該選項保存到cookie中。你的意思是它不會自動選擇以前選擇的語言嗎?如果是這樣的話,請參閱我的第二個代碼片段:「閱讀cookie ....無論您喜歡哪個事件」。根據此設置您的HTML元素。 –

+0

當我點擊西班牙語,然後刷新。語言回到英語。 ES也需要它。 – Shawn31313