Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('replaceI18nPlaceholders', () => {
dom = new JSDOM(`<!DOCTYPE html><html><head><title data-i18n="title"></title></head><body><span data-i18n="hello"></span><p>__MSG_world__</p></body></html>`);
global.document = dom.window.document;
global.Node = dom.window.Node;
global.NodeFilter = dom.window.NodeFilter;
global.browser = {
i18n: {
getMessage: (key) => ({ title: 'Title', hello: 'Hello', world: 'World' }[key])
Expand All @@ -20,6 +21,7 @@ describe('replaceI18nPlaceholders', () => {
dom.window.close();
delete global.document;
delete global.Node;
delete global.NodeFilter;
delete global.browser;
});
test('replaces placeholders in elements and text nodes', () => {
Expand Down
25 changes: 16 additions & 9 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,24 @@ function replaceI18nPlaceholders() {
});

// Replace __MSG_...__ patterns in common attributes (e.g., placeholder, title, aria-label)
document.querySelectorAll('*').forEach(el => {
if (!el.attributes) return;
Array.from(el.attributes).forEach(attr => {
if (typeof attr.value === 'string' && attr.value.includes('__MSG_')) {
const newVal = replaceTokens(attr.value);
if (newVal !== attr.value) {
el.setAttribute(attr.name, newVal);
if (document.documentElement) {
const walker = document.createTreeWalker(document.documentElement, NodeFilter.SHOW_ELEMENT);
let el = walker.currentNode;
while (el) {
if (el.hasAttributes()) {
for (let i = 0; i < el.attributes.length; i++) {
const attr = el.attributes[i];
if (typeof attr.value === 'string' && attr.value.includes('__MSG_')) {
const newVal = replaceTokens(attr.value);
if (newVal !== attr.value) {
el.setAttribute(attr.name, newVal);
}
}
}
}
});
});
el = walker.nextNode();
}
}

// Replace __MSG_...__ patterns in the document title
if (document.title && document.title.includes('__MSG_')) {
Expand Down