Is there a way to switch between languages on a website to replace text on multiple pages with a simple button click? What is the most efficient method for achieving this?
This code snippet only changes text in the first div. Is there a way to implement a language change feature that applies across the entire site?
var text1 = document.getElementById('english');
var text2 = document.getElementById('swedish');
const swap = () => {
if (text1.classList.contains("hidden")) { //if english contains class 'hidden'
text1.classList.remove("hidden"); //remove hidden class from english
text2.classList.add("hidden"); //add hidden class to swedish
} else {
text1.classList.add("hidden"); //add hidden class to english
text2.classList.remove("hidden"); //remove hidden class from swedish
}
}
.hidden {
display: none;
}
<button onclick="swap()">Switch Text</button>
<div class="content">
<div id="english">
<h1>Welcome</h1>
<p>English text</p>
</div>
<div id="swedish" class="hidden">
<h1>Vällkommen</h1>
<p>Svensk tekst</p>
</div>
</div>