将 Cookie 添加到语言转换器

发布于 2024-12-02 02:57:42 字数 2687 浏览 0 评论 0原文

我正在尝试为我的网站添加一些语言转换器。我希望它能使用 cookie 保存语言。

如何修改这段代码来实现这个目标?

<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>

I'm trying to add a little Language changer for my site. I'd like it to save the language using cookies.

How can this code be modified to achieve this goal?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2024-12-09 02:57:42

听起来您正在使用 MooTools。考虑使用 MooTools cookie 功能

当您完成语言切换后,将其放置在链接单击事件处理程序:

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

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

当您想要读取 cookie 时,可能是在页面加载时(或您喜欢的任何事件):

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

Sounds like you're using MooTools. Consider using the MooTools cookie functionality:

Place this when you're done switching the language in your link click event handler:

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

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

When you want to read the cookie, perhaps on page load (or whichever event you like):

alert('This user has their language set to: ' + Cookie.read('userLang'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文