有一些改進避免錯誤翻譯等或英文到英文問題。 - http://jsfiddle.net/F248G/3/
// 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);
}
};
// is language set? if so, auto translate
(function() {
// to avoid "lost in translation" on stacking up, i.e.
// translate from english to spanish, then from translated spanish back to english or others
// with errors, always use english as base language.
if (!translateDiv.retrieve("orig_en")) {
translateDiv.store("orig_en", translateDiv.get("html"));
}
// check cookie and if so, translate and set new base language
var toLang = Cookie.read("googleLang");
if (toLang && toLang != lang) {
google.language.translate(translateDiv.retrieve("orig_en"), lang, toLang, callback);
lang = toLang;
}
})();
// 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");
if (toLang === lang)
return;
// 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);
// ... and add here the code to save the last choice
Cookie.write("googleLang", toLang, {
path: "/"
});
});
});
當然
,你可以只看http://mootools.net/docs/core/Utilities/Cookie
嘿迪米塔爾 - 感謝這麼多的幫助。 – Pennf0lio 2011-05-11 21:57:54
我做得對(http://jsbin.com/ovage5)? – Pennf0lio 2011-05-11 21:59:27
沒有。更新的答案。顯然,你需要的不僅僅是片段...... tbh我只是重寫了這個,因爲我認爲我可以將這段代碼片段用作一些多語言工作的基礎。 – 2011-05-11 23:38:42