1

So, I have two webpages, html and html_en.
Is it possible to detect with JS system or browser language, and, if it's not italian, switch it automatically to english version?
I know there is a navigator.language(s) to help me with, but I dunno how to realise it together, in order to switch htmls if language is not it.
Thanks

Furious Mountain
  • 1,323
  • 3
  • 16
  • 44
  • you usually don't switch the entire pages if you are using plain HTML css and js, use a library that helps with localization there are a lot of them out there just do a simple google research – Ivan Ambla Mar 11 '22 at 10:31

1 Answers1

3

Ok, I found an answer.
Simple JS to it.

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";
    }
}