Skip to content Skip to sidebar Skip to footer

Js - Replace Language

i have following line in my JS-File: language : (div.lang && div.lang != '') ? div.lang : 'en', I want simply change the line in: language : (div.lang && div

Solution 1:

var languageEnum = {
  english: "en",
  german: "de",
  spanish: "es",
  french: "fr",
  latin: "la",
  italian: "it"
};

var pageConfiguration = {
  language: languageEnum.english; // note that this could be a user preference too and you could check the browser's language
};

$(document).ready(function() {
  $("#type").change(function(){
    pageConfiguration.language = $(this).val();
  });
});

  //...
  language : (div.lang && div.lang != "") ? div.lang : pageConfiguration.language,
  //...

Solution 2:

//GLOBALvarLANGUAGE = "en"; // if english is default//on bodyload
$("#type").change(function(){
  LANGUAGE = $(this).val();
});


//wherever you want to use it
language : (div.lang && div.lang != "") ? div.lang : LANGUAGE,

For the Comments:

  1. enum just wasting ressources (in my opinion ==> btw thanks ;) )
  2. use my code is based on jquery
  3. on bodyload can be put into jquery on domready, html on bodyload, etc

For pure JS:

// HTML:

<select id="selectLanguage" onchange="languageChange">

// JS//// Global Var in JS (placed on top)var LANGUAGE = "en";

//// function placed anywherefunction languageChange(){
    LANGUAGE = document.getElementById("selectLanguage").value;
}

//// wherever you want to use the line
language : (div.lang && div.lang != "") ? div.lang : LANGUAGE

BTW: //onload events //// Html way

//or//// JQ way
$(document).ready(function{})

// etc...

Post a Comment for "Js - Replace Language"