1

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.lang != "") ? div.lang : "de",

or

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

or

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

or

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

or

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

And I want to replace the line in my js-file when I choose a language:

   <select id="type" name="type">
    <option value="en">English</option>
    <option value="de">German</option>
    <option value="es">Spanish</option>
    <option value="fr">French</option>
    <option value="la">Latin</option>
    <option value="it">Italian</option>
   </select>

But I have no idea how to realize it :/ Someone can help? And one tip: I don't want to change the language for the visitor in the frontend. I just want to change it to have my whole script in the chosen language.

UPDATE

I have following in my JS-File:

   // Global Var in JS (placed on top)
   var LANGUAGE = "en";
   function languageChange(){ 
   LANGUAGE = document.getElementById("selectLanguage").value;
   }

And I have following in my HTML-File:

   <p style="float:left"><input style="border:1px solid #bbb" type="submit" value="Choose language" id="submit"></p>
   <p style="float:right; padding-right:20px">
   <select id="selectLanguage" onchange="languageChange">
   <option value="en">English</option>
   <option value="de">Deutsch</option>
   <option value="es">Español</option>
   <option value="fr">Français</option>
   <option value="la">Lingua latina</option>
   <option value="it">Italiano</option>
   </select>&nbsp;<input type="submit" value="Enter" id="submit"></p>

And it don't works.

3
  • Are you using server side languages (PHP, asp, python ..)? Commented Jul 14, 2013 at 12:45
  • PHP, yes. You think it's better to realize it with preg_replace? Commented Jul 14, 2013 at 17:08
  • Not necessarily, you can keep client settings on the client. Commented Jul 14, 2013 at 18:08

2 Answers 2

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,
  //...
Sign up to request clarification or add additional context in comments.

Comments

1
//GLOBAL
var LANGUAGE = "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 anywhere
function 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...

7 Comments

I would put the language variable into a configuration object.
But +1 for the answer :)
This looks nice. Thanks. And I just add my HTML-Code (in my Question)?
Bodyload... Don't works my JS-File don't built the site if i put it in, maybe a syntax fault? May you can say what I put in bodyload?
What is the error? The answer takes into consideration implicitly that jQuery is linked in your page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.