Try the online version here:
https://earzalien.github.io/website-multilanguage/
This project provides simple multilingual management for static websites.
It allows switching the content language using flag buttons and saves the chosen language in the browser.
index.html: HTML page withdata-keyattributes for managing translated texts.script.js: JavaScript script to load and apply translations from the JSON file.lang.json: JSON file containing translations (to be created as needed).css/,img/folders for styles and images.
- Clone the repository or download the files.
- Add your translations to the
lang.jsonfile in the following format:
{
"site-title": {
"fr": "Bienvenue sur notre site",
"en": "Welcome to our website",
"es": "Bienvenido a nuestro sitio web"
},
"menu-accueil": {
"fr": "Accueil",
"en": "Home",
"es": "Inicio"
}
}
- Update HTML elements with the
data-keyattribute corresponding to the keys inlang.json. - Load the page in a modern browser.
- Click a flag to change the language; the selection will be saved.
- To add a language:
- Add the language identifier (e.g.,
defor German,itfor Italian...) to each key in thelang.jsonfile, including the necessary translations. - Create a corresponding flag button in the
index.htmlfile with the appropriatedata-langattribute:
<button data-lang="de"><img src="img/flag-de.png" alt="Deutsch" /><button>
-
Place the new flag image file in the
img/folder (e.g.,img/flag-de.png). -
Make sure the image filename,
data-langattribute, and language identifier in the JSON all match.
- Place the flag image in the
img/folder: - Example:
img/flag-it.pngfor Italy - Add an HTML button in
index.htmlcorresponding to the new language: <button data-lang="it"> <img src="img/flag-it.png" alt="Italiano" /> </button> ``` - It is recommended to use images of similar size and format for a better appearance.
Asynchronous loading of translations from a JSON file. Dynamic updating of text content and placeholders. Saving the selected language with localStorage. Easy support for adding new languages and flag images.
Add other languages in lang.json. Add as many flags as needed in the img/ folder and update the HTML. Modify the CSS style as needed.
Integrate into any static HTML project.
Essayez la version en ligne ici : https://earzalien.github.io/website-multilanguage/
Ce projet est une gestion multilingue simple pour sites web statiques.
Il permet de changer la langue du contenu via des boutons drapeaux, et sauvegarde la langue choisie dans le navigateur.
index.html: page HTML avec attributsdata-keypour gérer les textes traduits.script.js: script JavaScript pour charger et appliquer les traductions depuis le fichier JSON.lang.json: fichier JSON contenant les traductions (à créer selon vos besoins).- Dossiers
css/,img/pour styles et images.
- Cloner le dépôt ou télécharger les fichiers.
- Ajouter vos traductions dans le fichier
lang.jsonau format suivant :
{
"site-title": {
"fr": "Bienvenue sur notre site",
"en": "Welcome to our website",
"es": "Bienvenido a nuestro sitio web"
},
"menu-accueil": {
"fr": "Accueil",
"en": "Home",
"es": "Inicio"
}
}
- Mettre à jour les éléments HTML avec l’attribut
data-keycorrespondant aux clés danslang.json. - Charger la page dans un navigateur moderne.
- Cliquer sur un drapeau pour changer la langue ; la sélection sera mémorisée.
- Pour ajouter une langue, il suffit :
- D’ajouter l’identifiant de langue (exemple :
depour allemand,itpour italien...) dans chaque clé du fichierlang.jsonavec les traductions nécessaires. - De créer un bouton drapeau correspondant dans le fichier
index.htmlavec l’attributdata-langadéquat :
- D’ajouter l’identifiant de langue (exemple :
<button data-lang="de"><img src="img/flag-de.png" alt="Deutsch" /><button>
-
De placer le fichier image du nouveau drapeau dans le dossier
img/(par exempleimg/flag-de.png). -
Veillez à maintenir la cohérence entre le nom de l’image, l’attribut
data-langet l’identifiant de langue dans le JSON.
- Placez l’image du drapeau dans le dossier
img/:- Exemple :
img/flag-it.pngpour l’Italie
- Exemple :
- Ajoutez un bouton HTML dans
index.htmlcorrespondant à la nouvelle langue :
<button data-lang="it"> <img src="img/flag-it.png" alt="Italiano" /> </button>
- Il est recommandé d’utiliser des images à taille et format similaires pour une meilleure apparence.
Chargement asynchrone des traductions depuis un fichier JSON. Modification dynamique du contenu textuel et des placeholders. Sauvegarde de la langue sélectionnée avec localStorage. Prise en charge facile de l’ajout de nouvelles langues et images de drapeaux.
Ajoutez d’autres langues dans lang.json. Ajoutez autant de drapeaux que nécessaire dans le dossier img/ et mettez à jour le HTML. Modifiez le style CSS selon vos besoins.
Intégrez dans tout projet HTML statique.