Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

load content on user language in javascript

document.addEventListener("DOMContentLoaded", () => {
  let userLanguage = navigator.languages
    ? navigator.languages[0]
    : navigator.language || navigator.userLanguage;
  const currentLanguage = document.documentElement.lang;
  const localeURI = "/docs/strings";

  switch (userLanguage.toLowerCase()) {
    case "zh-cn":
      userLanguage = "zh_CN";
      break;
    case "zh-tw":
      userLanguage = "zh_TW";
      break;
    case "zh":
      userLanguage = "zh_TW";
      break;
    case "pt-br":
      userLanguage = "pt_BR";
      break;
    default:
      userLanguage = userLanguage.substring(0, 2);
      break;
  }

  if (userLanguage && currentLanguage && userLanguage !== currentLanguage) {
    const langChangeDiv = document.getElementById("lang-change-div");
    const langChangeLink = document.getElementById("lang-change-a");
    const langChangeText = document.getElementById("lang-change-text");
    const strings = JSON.parse(get(localeURI));
    const responseCode = head(
      window.location.href.replace(`/${currentLanguage}/`, `/${userLanguage}/`)
    );

    if (
      strings[userLanguage].viewInLang &&
      strings[userLanguage].clickHere &&
      responseCode < 400
    ) {
      langChangeLink.href = window.location.pathname.replace(
        `/${currentLanguage}/`,
        `/${userLanguage}/`
      );
      langChangeLink.innerText = strings[userLanguage].clickHere;
      langChangeText.innerText = strings[userLanguage].viewInLang;
      langChangeDiv.style = null;
    } else {
      if (responseCode < 400) {
        console.log("Target page not found");
      } else {
        console.log("Strings not found for " + userLanguage);
        console.log("Page is " + currentLanguage);
      }
    }
  } else {
    console.error(`ul: ${userLanguage} cl: ${currentLanguage}`);
  }
});

function get(url) {
  const xhr = new XMLHttpRequest();
  xhr.open("GET", url, false);
  xhr.send();
  return xhr.responseText;
}

function head(url) {
  const xhr = new XMLHttpRequest();
  xhr.open("HEAD", url, false);
  xhr.send();
  return xhr.status;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: ohif add auth to config 
Javascript :: function multiply(a b) a * b javascript 
Javascript :: TypeError: Invalid schema configuration: `True` is not a valid type at path `id.required`. See https://bit.ly/mongoose-schematypes for a list of valid schema types.] 
Javascript :: how to get the first element in an array in javascript 
Javascript :: how to compile javascript class 
Javascript :: javascript dom functions 
Javascript :: node js orderby method 
Javascript :: if this then this, else that 
Javascript :: Plumsail - DataTable Cascading Dropdowns 
Javascript :: uncheck all other checkboxes when one is checked 
Javascript :: return the remainder from two numbers javascript 
Javascript :: filter by last week 
Javascript :: laravel onkeyup textbox, get value from span javascript 
Javascript :: javascript How to show array content in output window 
Javascript :: getting json from response using getSync method 
Javascript :: angularjs Both ng-model and ng-change on input alter the $scope state - which one takes priority 
Javascript :: angularjs getting Error: [$rootScope:inprog] $digest already in progress when changed from Fetch to $http + $q 
Javascript :: Understanding higher order JavaScript functions 
Javascript :: React Native : Add a band of color in the background 
Javascript :: socket io check send 
Javascript :: I have a dataframe with a json substring in 1 of the columns. i want to extract variables and make columns for them 
Javascript :: Streaming search queries with Node.js and Socket.io (streaming to a given socket 
Javascript :: open 2 links with one click html jquery 
Javascript :: laravel sending email to outlook link not working 
Javascript :: javasrccipt loop array 
Javascript :: how to install ghost js 
Javascript :: A Nodule Module For ExpressJS 
Javascript :: routing/switches 
Javascript :: Declaring A Method Outside The Constructor 
Javascript :: jquery properties 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =