首页 > 解决方案 > 使用 jQuery 为多语言站点设置 cookie 和重定向

问题描述

不确定这是否甚至可以使用 cookie 来完成,但我觉得使用我当前的代码已经很接近了。我有一个多语言网站(英语和法语),我试图在用户“登录”时设置一个 cookie,它会在登录成功时设置一个 cookie。如果用户从未登录并尝试访问另一个页面在该站点中,它现在将它们重定向到英文“登录”页面。问题是,如果他们尝试访问法语页面,它仍然会将他们重定向到英语“登录”。如果这有意义的话,我希望它能够将他们重定向到他们尝试访问的语言。所有法语页面与英语页面具有相同的名称,但添加了“-fr.html”,因此“index-fr.html”是法语索引等等。

这很接近,但它正在创建一个坏循环

jQuery:

 cookie set on ajax success: _siteNS.Utils.setCookie("x-access", true, 365);

 function checkCookie() {
    var current_url = window.location.pathname;
    var logInCookie = _siteNS.Utils.readCookie("x-access");

    //if the cookie is false and location is not the landing page set location to landing page
    if (!logInCookie && current_url != "/landing.html") {
      window.location.replace("/landing.html");
    }
    if (window.location.href.indexOf("-fr.html") > -1 && !logInCookie) {
      window.location.replace("/landing-fr.html");
    }
  }

标签: javascriptjquery

解决方案


推荐阅读