let language = navigator.language;
switch (language) {
case es :
document.getElementById("demo").innerText = "Hola ";
break;
case en :
document.getElementById("demo").innerText = "Hello ";
}
Am not receiving anything back.
I would appreciate if someone knows why it isn’t working
2
Answers
I am assuming your html looks like this:
and my js file:
You need to add quotes around you language switch case ‘es’ not es … Because navigator.language returns a string and if you don’t use quotes javascript is going to be looking for a variable or a function call.
when I made those modifications your code worked just fine.
You will need to wrap quotes around the possible values and you need to parse the
language
value to look like the values in your switch case:Note that instead of
case 'en' :
you could usedefault :
which would use the English language as a default even if the language has a value that your code did not expect.