Skip to content

Instantly share code, notes, and snippets.

@thedivtagguy
Created August 5, 2025 05:51
Show Gist options
  • Save thedivtagguy/d155054ce88a631b8d85474a659a7b15 to your computer and use it in GitHub Desktop.
Save thedivtagguy/d155054ce88a631b8d85474a659a7b15 to your computer and use it in GitHub Desktop.
{
"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!"
}
{
"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!"
}
<!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