42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
export function t(key, ...args) {
|
|||
|
|
const msg = chrome.i18n.getMessage(key, args);
|
||
|
|
return msg || key;
|
||
|
|
}
|
||
|
|
export function applyI18n(root = document) {
|
||
|
|
root.querySelectorAll("[data-i18n]").forEach((el) => {
|
||
|
|
const key = el.dataset.i18n || "";
|
||
|
|
if (el.children.length === 0) {
|
||
|
|
const msg = chrome.i18n.getMessage(key);
|
||
|
|
if (msg)
|
||
|
|
el.textContent = msg;
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
const msg = chrome.i18n.getMessage(key);
|
||
|
|
if (msg) {
|
||
|
|
const firstText = el.firstChild;
|
||
|
|
if (firstText && firstText.nodeType === Node.TEXT_NODE) {
|
||
|
|
firstText.nodeValue = msg;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
root.querySelectorAll("[data-i18n-placeholder]").forEach((el) => {
|
||
|
|
const key = el.dataset.i18nPlaceholder || "";
|
||
|
|
const msg = chrome.i18n.getMessage(key);
|
||
|
|
if (msg)
|
||
|
|
el.placeholder = msg;
|
||
|
|
});
|
||
|
|
root.querySelectorAll("[data-i18n-title]").forEach((el) => {
|
||
|
|
const key = el.dataset.i18nTitle || "";
|
||
|
|
const msg = chrome.i18n.getMessage(key);
|
||
|
|
if (msg)
|
||
|
|
el.title = msg;
|
||
|
|
});
|
||
|
|
root.querySelectorAll("[data-i18n-aria]").forEach((el) => {
|
||
|
|
const key = el.dataset.i18nAria || "";
|
||
|
|
const msg = chrome.i18n.getMessage(key);
|
||
|
|
if (msg)
|
||
|
|
el.setAttribute("aria-label", msg);
|
||
|
|
});
|
||
|
|
}
|