【问题标题】:Google translate save language setting to a cookie谷歌将保存语言设置翻译成 cookie
【发布时间】:2011-05-11 12:24:54
【问题描述】:

是否可以将 google.translate 的设置保存在 cookie 中?例如,我将语言从“英语”设置为“西班牙语”并导航到我网站的其他页面(例如关于),它仍将其语言保留为“西班牙语”。我需要一些关于如何实现它的帮助,我知道这是可能的,我只是不知道如何正确实现它。

Here's (http://jsbin.com/esiga3) 我目前正在工作的代码”。我需要它来检测是否有为这种语言设置的 cookie,如果没有,它会创建一个cookie来设置语言。

我认为可以使用一些javascript或cookie在用户端临时设置google translate api的语言设置。

谢谢!

【问题讨论】:

    标签: javascript cookies mootools google-translate


    【解决方案1】:

    进行了一些改进,以避免翻译错误等或英语到英语的问题。 - 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

    【讨论】:

    • 嘿 Dimitar - 非常感谢您的帮助。
    • 没有。更新的答案。显然,您需要的不仅仅是片段......我只是重写这个,因为我认为我可以将这个代码 sn-p 用作一些多语言工作的基础。
    • 你太棒了,非常感谢你的帮助!我一直在网上挖掘这个问题的一些好的参考,但我找不到任何好的参考。是的,你的 sn-p 真的很方便 :) 再次感谢。
    猜你喜欢
    • 2010-12-18
    • 2013-12-28
    • 2016-08-10
    • 2015-03-29
    • 1970-01-01
    • 2021-07-07
    • 2018-04-07
    • 2019-05-29
    相关资源
    最近更新 更多