我目前正在编写一个网站,该代码以不同的语言显示:
var userLang = navigator.language || navigator.userLanguage; if (userLang.substring(0, 2) == "zh") { location.href = "index_ch.html"; }
但是使用这个javascript有一个缺点,就是当我的浏览器是英文时,我无法手动使用中文显示,反之亦然。我只想知道如何:
谢谢
答案 0 :(得分:0)
您可以使用localStorage
执行此操作// Test if localStorage is supported by the user's browser
var storageAllowed = typeof(Storage) !== "undefined";
if (storageAllowed) {
// get cookie
var userLangCodeCookie = localStorage.getItem("userLangCode");
// Redirect if exists and not falsy
if (userLangCodeCookie) {
location.href = "index_" + userLangCodeCookie + ".html";
}
}
var userLang = navigator.language || navigator.userLanguage;
var userLangCode = userLang.substring(0, 2);
if (userLangCode == "zh") {
if (storageAllowed) {
// Set cookie
localStorage.setItem("userLangCode", 'ch');
}
location.href = "index_ch.html";
}