From c4d85a4c12da23208b7a497cd0673e48067e5c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20dBruxelles?= <18559798+jdbruxelles@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:37:08 +0200 Subject: [PATCH] Add alt attribute support for images (using the data-si18n-alt attribute) and small improvements --- CHANGELOG.md | 7 ++++- src/si18n.js | 69 +++++++++++++++++------------------------ website/locales/en.json | 2 +- website/locales/fr.json | 2 +- 4 files changed, 36 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2983d45..a9abb99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ > breaking changes and/or new features that you need to implement in your > project. +## 1.4.4 (June 5, 2024) + +- Add `alt` attribute support for auto translating using the `data-si18n-alt` attribute with value `true` in the element (e.g.: `` tag). +- Fix: Skip elements with `data-si18n` attribute not set, instead of breaking the whole localization process. + ## 1.4.3 (January 13, 2024) - Fixed a bug where the name of the saved language (via `saveLang`, `saveAs`) would be corrupted on the user side and not available, causing the page to spit out. @@ -17,7 +22,7 @@ - Slight performance improvement. - Improve documentation and demo site. -## 1.4.1 (October, 2023) +## 1.4.1 (October 30, 2023) - Ignore `data-si18n` attributes with no value. - Functions (`translate` and `callback`) are excluded from the result of the diff --git a/src/si18n.js b/src/si18n.js index 05b2fe7..61a04f4 100644 --- a/src/si18n.js +++ b/src/si18n.js @@ -317,50 +317,37 @@ export default class Si18n { // if you want use the string for other purposes // (e.g.: title, aria-label attributes, etc.). const htmlTags = document.querySelectorAll("[data-si18n]"); - if (htmlTags.length > 0) { - htmlTags.forEach((element) => { - const { - si18nDefault, - si18nTitle, - si18nLabel, - si18nHtml, - si18nValue, - si18nContent, - si18nAriaLabel, - si18nPlaceholder - } = element.dataset; - const JSONPath = element.dataset.si18n; - if (!JSONPath) return; // If the data-si18n attribute is empty. - const text = this.t(JSONPath); - - if (si18nDefault !== "false") { - if (si18nHtml === "true") element.innerHTML = text; + const tagsLength = htmlTags.length; + + if (tagsLength > 0) { + // Supported attributes in which the text can be automatically set: + const attributesMap = { + title: "si18nTitle", + label: "si18nLabel", // e.g: for + alt: "si18nAlt", // e.g: for + "aria-label": "si18nAriaLabel", + value: "si18nValue", // e.g: for ,