所以,我有两个网页,html和html_en。
是否可以用JS系统或浏览器语言检测,如果不是意大利语,可以自动切换到英文版吗?
我知道有一个navigator.language可以帮助我,但我不知道如何一起实现它,以便切换html,如果语言不是它。
谢谢
发布于 2022-03-11 11:02:27
好吧,我找到答案了。
简单的JS。
let language = window.navigator.language;
let languageFistTwo = language.substr(0, 2); // To only keep the first 2 characters.
let currentLocation = document
.getElementsByTagName("html")[0]
.getAttribute("lang-js");
switch (languageFistTwo) {
case "it":
if (currentLocation == "it") window.location.href = "../../index-test.html";
break;
default:
if (currentLocation != "en") {
window.location.href = "../../index.html";
}
}
https://stackoverflow.com/questions/71437107
复制相似问题