Basic Internationalization Script for Static HTML
This little script is written for very basic static HTML internationalization with no dependency and pure JavaScript.
How to use
Add an element to your HTML file with data-i18n="key"
attribute.
<h1 data-i18n="hello"><h1>
Import the script.
<script type="text/javascript" src="script.js"></script>
Create localization files.
en.json
{
"hello": "Hello, World!"
}
tr.json
{
"hello": "Merhaba, Dünya!"
}
Apply different languages with different buttons or images or any other element.
<button onclick="applyTranslation('tr')">🇹🇷</button>
<button onclick="applyTranslation('en')">🇬🇧</button>
Enjoy!
Specifications
- Memoization - does not download the same resource twice.
- Async - does not block the main thread.
- No dependency - does not contain bloatware.
- Pure JavaScript - no need to transpile.
- Remembers choice - makes use of the local storage API.