I have created a simple portfolio for myself which is currently in English. I also want to have it in German and other languages. To this end, I have an dropdown option box with the respective language. What I want to do is, once a language is selected, a javascript EventListener will notice this and change the language of the website to that language. I have a en.json and de.json file with the respective class name of the text and the text itself.
The dropdown option menu:
<select id="language" class="language">
<option>🏴 ENG</option>
<option>🇩🇪 DE</option>
<!-- <option>🇫🇷 FR</option> -->
<!-- <option>🇱🇺 LUX</option> -->
</select>
The following is an example of a div containing the class "RecentGrad" and "Name" as well as a sample of the json file structures. I have multiple texts to translate but I am only going to show one here for understanding:
<div class="header-text">
<p class="RecentGrad"> Recent graduate & aspiring developer</p>
</div>
German json file:
{
"RecentGrad": "Hochschulabsolvent & angehender Entwickler"
}
English json file:
{
"RecentGrad": "Recent graduate & aspiring developer"
}
I have the barebones of a javascript done which listens for a change and is then supposed to do an action based on this change. I am relatively new to HTML, CSS, and JavaScript so I don't know how to go from here. I have looked at multiple tutorials and other stackoverflow topics with multilingual websites but I can't seem to get any of them to work.
<script>document.querySelector('#language').addEventListener("change", function() {
if (this.value == "🏴 ENG") {
// change all text to ENG
}else if (this.value == "🇩🇪 DE"){
// change all text to DE
// }else if (this.value == "🇫🇷 FR"){
// change all text to FR
// }else if (this.value == "🇱🇺 LUX"){
// change all text to LUX
}
});</script>