Skip to content

Commit

Permalink
add function to switch the language (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuprosov authored Apr 13, 2024
1 parent e17c40b commit 9367578
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions languageSelect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This functionality can be applied for any text on the page, but would require readily available translated texts, saved in 'languageContent' with the relevant keys. Those keys must be used as id in html layout to access them.
39 changes: 39 additions & 0 deletions languageSelect/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>Switching the language of web page using JavaScript</h2>
<div id = "text1"> This is a sample content </div>
<div id = "language"> English </div>
<div id = "languageSwitcher">
<button onclick = "swithcLang('en')"> English </button>
<button onclick = "swithcLang('fr')"> French </button>
<button onclick = "swithcLang('es')"> Spanish </button>
</div>
<script>
let languageContent = {
"en": {
"text1": "This is a sample content",
"language": "English",
},
"fr": {
"text1": "Ceci est un contenu d'exemple",
"language": "Français",
},
"es": {
"text1": "Este es un contenido de ejemplo",
"language": "Español",
}
}
function swithcLang(lang) {
for (let key in languageContent[lang]) {
document.getElementById(key).innerHTML = languageContent[lang][key];
}
}
</script>
</body>
</html>

0 comments on commit 9367578

Please sign in to comment.