Last active
August 25, 2022 09:57
-
-
Save FANMixco/93549a24c75cf8e9a96dd5b827202f4f to your computer and use it in GitHub Desktop.
A simple solution for translating static websites with JS and a data attribute (data-translation)
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
These scripts allow people to build static pages with multilingual support without any framework or special libs. |
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
const getScript = url => new Promise((resolve, reject) => { | |
const script = document.createElement('script'); | |
script.src = url; | |
script.async = true; | |
script.onerror = reject; | |
script.onload = script.onreadystatechange = function() { | |
const loadState = this.readyState; | |
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return; | |
script.onload = script.onreadystatechange = null; | |
resolve(); | |
} | |
document.head.appendChild(script); | |
}); |
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
const translations = { | |
"telephone": "Telephone", | |
"email": "Email", | |
"contactMe": "Contact me" | |
}; |
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
const translations = { | |
"telephone": "Teléfono", | |
"email": "Email", | |
"contactMe": "Contáctame" | |
}; |
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
<script src="get-script.js"></script> | |
<script src="translations.js"></script> | |
<p data-translation="contactMe"></p> | |
<p data-translation="telephone"></p> | |
<p data-translation="email"></p> |
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
let lang = 'en'; | |
let nLang = (navigator.languages | |
? navigator.languages[0] | |
: (navigator.language || navigator.userLanguage)).split('-')[0]; | |
let supportedLang = ['en', 'es']; | |
lang = supportedLang.includes(nLang) ? nLang : lang; | |
getScript(`lang-${lang}.js`) | |
.then(() => { | |
document.querySelectorAll('[data-translation]').forEach(item => { | |
item.innerHTML = translations[`${item.dataset.translation}`]; | |
}); | |
}) | |
.catch((e) => { | |
console.error(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment