Created
August 5, 2025 05:51
-
-
Save thedivtagguy/d155054ce88a631b8d85474a659a7b15 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"title": "Welcome to Our Website", | |
"description": "This is some content on our website that we want to translate into multiple languages.", | |
"contact": "Contact Us", | |
"footer": "Thank you for visiting our website!" | |
} |
This file contains hidden or 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
{ | |
"title": "Bienvenue sur Notre Site Web", | |
"description": "Ceci est du contenu sur notre site web que nous voulons traduire en plusieurs langues.", | |
"contact": "Nous Contacter", | |
"footer": "Merci de visiter notre site web!" | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<style> | |
body { | |
font-family: Arial, sans-serif; | |
max-width: 600px; | |
margin: 50px auto; | |
padding: 20px; | |
} | |
button { | |
padding: 10px 15px; | |
margin: 5px; | |
border: none; | |
background: #007bff; | |
color: white; | |
border-radius: 5px; | |
cursor: pointer; | |
} | |
button:hover { | |
background: #0056b3; | |
} | |
.content { | |
margin-top: 30px; | |
} | |
</style> | |
</head> | |
<body> | |
<button onclick="loadLang('en')">English</button> | |
<button onclick="loadLang('fr')">Français</button> | |
<div class="content"> | |
<h1 data-key="title">Welcome</h1> | |
<p data-key="description">Loading...</p> | |
<button data-key="contact">Contact</button> | |
<p data-key="footer">Thank you!</p> | |
</div> | |
<script> | |
const cache = {}; | |
async function loadLang(code) { | |
if (cache[code]) { | |
applyTranslations(cache[code]); | |
return; | |
} | |
try { | |
const response = await fetch(`lang/${code}.json`); | |
const translations = await response.json(); | |
cache[code] = translations; | |
applyTranslations(translations); | |
} catch (error) { | |
console.error('Failed to load translations:', error); | |
} | |
} | |
function applyTranslations(translations) { | |
document.querySelectorAll('[data-key]').forEach(el => { | |
const key = el.getAttribute('data-key'); | |
if (translations[key]) { | |
el.textContent = translations[key]; | |
} | |
}); | |
} | |
// Load default language | |
loadLang('en'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment