A lightweight, client-side internationalization (i18n) library for JavaScript that enables multi-language support for websites with zero server-side dependencies.
- Zero Dependencies: Pure JavaScript implementation
- Client-Side Only: No server-side code required
- No CORS Issues: Language configuration embedded directly in the script
- Easy Integration: Simple HTML tags for translation
- Image Localization: Change images based on language
- Automatic Translation: Translates entire pages on load
- Multiple Languages: Support for unlimited languages
- Lightweight: Minimal footprint for fast loading
- Synchronous: No async/await complexity
- Clone or Download this repository to your project folder
- Include the translator script in your HTML:
<script type="text/javascript" src="./translator.js" zlangu="fr"></script>
- Edit
translator.jsto add your translations in theZLANG_CONFIGobject - Use
<zlang>tags in your HTML for translatable content
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Multi-Language Site</title>
<!-- Set the language using zlangu attribute -->
<script type="text/javascript" src="./translator.js" zlangu="fr"></script>
</head>
<body>
<h1><zlang key="title"></zlang></h1>
<p><zlang key="greeting"></zlang>, <zlang key="name"></zlang>!</p>
<p><zlang key="farewell"></zlang></p>
<!-- Language-specific image -->
<img zlang-img="logo" src="./images/default-logo.png" alt="Logo">
</body>
</html>multi_region/
βββ index.html # Example HTML page with translations
βββ translator.js # Main translation library (includes embedded config)
βββ README.md # This documentation
Edit the ZLANG_CONFIG object in translator.js to define your translations:
const ZLANG_CONFIG = {
"en": {
"title": "My Website",
"greeting": "Hello",
"name": "User",
"farewell": "Goodbye"
},
"es": {
"title": "Mi Sitio Web",
"greeting": "Hola",
"name": "Usuario",
"farewell": "AdiΓ³s"
},
"fr": {
"title": "Mon site web",
"greeting": "Bonjour",
"name": "Utilisateur",
"farewell": "Au revoir"
}
};To localize images, add language-specific image paths to the ZLANG_IMAGES object in translator.js:
const ZLANG_IMAGES = {
"en": {
"logo": "./images/logo-en.png",
"banner": "./images/banner-en.jpg",
"hero": "./images/hero-en.webp"
},
"es": {
"logo": "./images/logo-es.png",
"banner": "./images/banner-es.jpg",
"hero": "./images/hero-es.webp"
},
"fr": {
"logo": "./images/logo-fr.png",
"banner": "./images/banner-fr.jpg",
"hero": "./images/hero-fr.webp"
}
};In your HTML, use the zlang-img attribute to mark images for localization:
<img zlang-img="logo" src="./images/default-logo.png" alt="Logo">
<img zlang-img="banner" src="./images/default-banner.jpg" alt="Banner">
<img zlang-img="hero" src="./images/default-hero.webp" alt="Hero">The script will automatically update the src attribute based on the selected language.
Specify the target language using the zlangu attribute in the script tag:
<!-- English -->
<script src="./translator.js" zlangu="en"></script>
<!-- Spanish -->
<script src="./translator.js" zlangu="es"></script>
<!-- French -->
<script src="./translator.js" zlangu="fr"></script>Use <zlang> tags with a key attribute to mark translatable text content:
<zlang key="your_translation_key"></zlang>Use the zlang-img attribute on <img> tags to swap images based on language:
<!-- The src will be replaced with the language-specific image -->
<img zlang-img="logo" src="./images/default-logo.png" alt="Logo">
<img zlang-img="banner" src="./images/default-banner.jpg" alt="Banner">The src attribute serves as a fallback if no image is defined for the current language.
The script scans all <script> tags to find the one with the zlangu attribute and extracts the target language.
let lenOf = document.getElementsByTagName("script").length;
let useLang;
for (let i = 0; i < lenOf; i++) {
ourSrc = document.getElementsByTagName("script")[i];
if (ourSrc.hasAttribute("zlangu")) {
useLang = ourSrc.getAttribute('zlangu');
}
}All translations and images are embedded directly in the script, eliminating CORS issues:
const ZLANG_CONFIG = { /* text translations */ };
const ZLANG_IMAGES = { /* image URLs */ };The main class that handles all translation operations:
- Constructor: Initializes with target language
- availableLanguage(): Returns the embedded language data
- availableImages(): Returns the embedded image data
- translateText(key): Returns translated text for a specific key
- getImage(key): Returns image URL for a specific key
- translatePage(): Automatically translates all
<zlang>elements and[zlang-img]images on the page
On DOM ready, the script automatically:
- Instantiates the translator with the detected language
- Scans for all
<zlang>elements and replaces their content - Scans for all
[zlang-img]elements and updates theirsrcattribute
document.addEventListener('DOMContentLoaded', initializeTranslation);1. Page loads β Script detects zlangu attribute
2. DOM ready β Translator initializes
3. Read embedded ZLANG_CONFIG and ZLANG_IMAGES
4. Find all <zlang> tags β Replace text content
5. Find all [zlang-img] elements β Update image sources
The library includes robust error handling for:
- Non-existent languages
- Missing translation keys
- Missing image keys (gracefully falls back to default src)
new zlanguageTranslator(language)- language (string): Target language code (e.g., 'en', 'es', 'fr')
Returns the language configuration data.
const config = translator.availableLanguage();Returns the image configuration data.
const images = translator.availableImages();Translates a specific key to the target language.
const text = translator.translateText('greeting');Gets the image URL for a specific key in the target language.
const imageUrl = translator.getImage('logo');Automatically translates all <zlang> elements and updates all [zlang-img] images on the page.
translator.translatePage();-
Add text translations to
ZLANG_CONFIGintranslator.js:const ZLANG_CONFIG = { // existing languages... "de": { "title": "Meine Website", "greeting": "Hallo", "name": "Benutzer", "farewell": "Auf Wiedersehen" } };
-
Add language-specific images to
ZLANG_IMAGESintranslator.js(optional):const ZLANG_IMAGES = { // existing languages... "de": { "logo": "./images/logo-de.png", "banner": "./images/banner-de.jpg" } };
-
Use the new language by setting
zlangu="de"in your script tag
For dynamic language switching, you can programmatically change languages:
function switchLanguage(newLang) {
const translator = new zlanguageTranslator(newLang);
translator.translatePage();
}
// Switch to Spanish
switchLanguage('es');- Multi-language websites without server-side complexity
- Static site generators requiring i18n support
- Client-side applications needing quick translation
- Prototyping multilingual interfaces
- Educational projects demonstrating i18n concepts
- No network requests: All data is embedded, eliminating HTTP latency
- No CORS issues: Works on any server or local files
- Synchronous execution: Faster page rendering
- Minimal DOM manipulation: Efficient element updates
- Single script file: Reduces HTTP requests
- Fork the repository
- Add your language to
ZLANG_CONFIGintranslator.js - Optionally add images to
ZLANG_IMAGES - Test with your language using
zlanguattribute - Submit a pull request
This project is open source and available under the MIT License.
Translations not appearing?
- Verify the language code in
zlanguattribute matches a key inZLANG_CONFIG - Ensure translation keys exist in the config object
- Check that
<zlang>tags have thekeyattribute
Images not changing?
- Verify the language has entries in
ZLANG_IMAGES - Ensure the
zlang-imgattribute value matches a key in the image config - Check that image paths are correct
Console errors?
- Check browser console for specific error messages
- Verify JavaScript syntax in
translator.js
Need help? Open an issue in the repository with details about your setup and the problem you're experiencing.
Made with β€οΈ for the global web community