Creating a website and aiming to utilize JavaScript for the following:
If the browser language is set to French, retrieve the text from languageFr.json, otherwise, retrieve the text from languageEn.json.
Here is a snippet of the code I have so far:
JSON File
{
"h2": "random title",
"p": lorem ipsum
}
JavaScript Code within the head of index.html
<script text="text/javascript">
var lang;
function detect() {
var userLang = navigator.language || navigator.userLanguage;
if (userLang == "fr") {
lang = JSON.parse("languageFr.json");
} else {
lang = JSON.parse("languageEn.json");
}
}
$(document).ready(detect());
</script>
Following this, in the body of my HTML:
<h2><script>document.write(lang.h2);</script></h2>
Despite this, the code doesn't seem to be functioning correctly. The console in Chrome indicates that lang is undefined. What could be the issue here?
Appreciate the assistance.