-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function to switch the language (#479)
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |