javascript - Google translate save language setting to a cookie -
will possible save settings of google.translate in cookie? example set language 'english' 'spanish' , navigate other pages (eg. about) of site , still retain language 'spanish'. need on how implement this, know it's possible, don't know how implement it.
here's (http://jsbin.com/esiga3) code i'm working". need detect if theres cookie has been set language , if not create cookie set language.
i think possible set language setting of google translate api temporarily in user side using javascript or cookie.
thank you!
with few improvements avoid bad translations etc or english english issues. - http://jsfiddle.net/f248g/3/
// set original/default language var lang = "en"; var currentclass = "currentlang"; // load language lib google.load("language", 1); // when dom ready.... window.addevent("domready", function() { // retrieve div translated. var translatediv = document.id("languageblock"); // define function switch currentlanguage var callback = function(result) { if (result.translation) { translatediv.set("html", result.translation); } }; // language set? if so, auto translate (function() { // avoid "lost in translation" on stacking up, i.e. // translate english spanish, translated spanish english or others // errors, use english base language. if (!translatediv.retrieve("orig_en")) { translatediv.store("orig_en", translatediv.get("html")); } // check cookie , if so, translate , 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 click listener update div $$("#languages a").addevent("click", function(e) { // stop event if (e) e.stop(); // "to" language var tolang = this.get("rel"); if (tolang === lang) return; // set translation motion google.language.translate(translatediv.get("html"), lang, tolang, callback); // set new language lang = tolang; // add class current this.getsiblings().removeclass(currentclass); this.addclass(currentclass); // ... , add here code save last choice cookie.write("googlelang", tolang, { path: "/" }); }); });
of course, @ http://mootools.net/docs/core/utilities/cookie
Comments
Post a Comment