localizeHTML function exported
Last updated: 2026-03-01T23:25:47.122Z
Location
Metrics
LOC: 25
Complexity: 5
Params: 1
Signature
localizeHTML(root: Document | Element = document): : void
Summary
Processes all elements with i18n data attributes in root and replaces their content/attributes with the localized strings. Supported attributes: data-i18n → sets element.textContent data-i18n-title → sets element.title data-i18n-placeholder → sets (element as HTMLInputElement).placeholder data-i18n-aria-label → sets element.ariaLabel
Source Code
export function localizeHTML(root: Document | Element = document): void {
root.querySelectorAll<HTMLElement>("[data-i18n]").forEach((el) => {
const msg = t(el.dataset.i18n!);
if (msg) el.textContent = msg;
});
root.querySelectorAll<HTMLElement>("[data-i18n-title]").forEach((el) => {
const msg = t(el.dataset.i18nTitle!);
if (msg) el.title = msg;
});
root
.querySelectorAll<
HTMLInputElement | HTMLTextAreaElement
>("[data-i18n-placeholder]")
.forEach((el) => {
const msg = t((el as HTMLElement).dataset.i18nPlaceholder!);
if (msg) el.placeholder = msg;
});
root.querySelectorAll<HTMLElement>("[data-i18n-aria-label]").forEach((el) => {
const msg = t(el.dataset.i18nAriaLabel!);
if (msg) el.setAttribute("aria-label", msg);
});
}