代码之家  ›  专栏  ›  技术社区  ›  The Dead Man

根据浏览器语言重定向

  •  0
  • The Dead Man  · 技术社区  · 7 年前

    当用户打开我的网站时,这是我想要的行为:

    • 如果浏览器语言=英语,则切换到波兰语。
    • 如果浏览器语言=波兰语,则不执行任何操作。
    • 假设波兰语是默认语言。

    因为它是一个非常简单的应用程序,所以我使用jQuery进行翻译。

    以下是我目前的解决方案:

    J中间: demo

    //redirect
    $(document).ready(function() {
      var userLang = navigator.language || navigator.userLanguage;
      if (userLang == "pl-pl") {
        break;
      } else {
        userLang = "eng-gb"
      }
    }
    });
    
    
    
    // translation
    
    // The default language is Polish
    var lang = "pl-pl";
    // Check for localStorage support
    if ('localStorage' in window) {
    
      var usrLang = localStorage.getItem('uiLang');
      if (usrLang) {
        lang = usrLang
      }
    }
    
    
    var arrLang = {
      "pl-pl": {
        "Valentine": "Do Walentynek zostało już tylko",
      },
      "en-gb": {
        "valentine": "Remaining days to Valentines!",
      }
    }
    
    
    $(document).ready(function() {
      $(".lang").each(function(index, element) {
        $(this).text(arrLang[lang][$(this).attr("key")]);
      });
    
      $(".lang").each(function(index, element) {
        $(this).text(arrLang[lang][$(this).attr("key")]);
      });
    
      // get/set the selected language
      $(".translate").click(function() {
        var lang = $(this).attr("id");
    
        // update localStorage key
        if ('localStorage' in window) {
          localStorage.setItem('uiLang', lang);
        }
    
        $(".lang").each(function(index, element) {
          $(this).text(arrLang[lang][$(this).attr("key")]);
        });
      });
    });
    <div class="translate">
      <ul class="language">
        <li class="eng translate" id="en-gb">EN</li>
        <li class="pl translate" id="pl-pl">PL</li>
      </ul>
    </div>
    
    <div class="rss-feed">
      <p class="lang" key="valentine">Do Walentynek zostało już tylko
      </p>
      <span id="mybenfit-timer" class="lang" key="days">
           </span>
    </div>

    不幸的是,我的解决方案不起作用。我的代码怎么了?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Kossi D. T. S.    7 年前

    $(document).ready(function() {
    
        // translation
    
        // The default language is English
        var userLang = navigator.language || navigator.userLanguage;
        var lang = "pl-pl";
    
        // If Browser Language == English then switch to the Polish language.
        // since there en-GB en-US ...
        if(userLang.split('-')[0] == 'en'){ 
            lang = "pl-pl";
        }
    
        // Check for localStorage support
        if('localStorage' in window){
    
            var usrLang = localStorage.getItem('uiLang');
            if( typeof(usrLang) === 'string' && usrLang !== 'undefined') {
                lang = usrLang
            }
    
        }
    
        var arrLang = {
            "pl-pl": {
                "valentine": "Do Walentynek zostało już tylko",
                "days": " 10 dni"
            },
            "en-gb": {
                "valentine": "Remaining days to Valentines!",
                "days": " 10 days"
            }
        }
    
    
        $(".lang").each(function(index, element) {
            $(this).text(arrLang[lang][$(this).attr("key")]);
        });
    
        // get/set the selected language
        $(".translate").click(function(e) {
            e.stopPropagation();
            var lang = $(this).attr("id");
    
            // update localStorage key
            if('localStorage' in window){
                localStorage.setItem('uiLang', lang);
            }
    
            $(".lang").each(function(index, element) {
                $(this).text(arrLang[lang][$(this).attr("key")]);
            });
        });
    });
    
        2
  •  1
  •   Mohamed-Yousef    7 年前
    • userLang en-GB 所以语言是两个小写字符,国家是两个大写字符,所以if语句应该是 if(userLang == 'pl-PL') 不 if(userLang == 'pl-pl') 它总是会以你的方式返回错误

    • 但是当你只需要语言的时候,对吗?你可以试试这个。。并且总是用两个小写字母来表达。。不需要使用 pl-pl 或 英语-国标 你可以用 pl en

    //redirect
    $(document).ready(function() {
      var userLang = navigator.language || navigator.userLanguage;
      console.log(userLang);
      var userLang = userLang.split('-')[0];
      console.log(userLang);
      var userLang = (userLang !== "pl")? 'en' : 'pl';
      console.log(userLang);
      
      var arrLang = {
        "pl": {
          "Valentine": "Do Walentynek zostało już tylko",
        },
        "en": {
          "valentine": "Remaining days to Valentines!",
        }
      }
      console.log(arrLang[userLang].valentine);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        3
  •  0
  •   Cue    7 年前

    属性名区分大小写。

    arrLang 与英语不同。 pl-pl Valentine 虽然 en-gb 是 valentine . 但是你的 key="valentine" 它不存在。

    把它们都小写。

    var arrLang = {
      "pl-pl": {
        "valentine": "Do Walentynek zostało już tylko",
      },
      "en-gb": {
        "valentine": "Remaining days to Valentines!",
      }
    }