Is there an easy way make popup/content/options pages multilingual?
The documentation gave me an impression that the only way is to use JS to add localized strings into html.
This would make things very cumbersome and complicated (each node would need an ID or class, etc)
I've tried use ${
tag
}
in HTML but it just displayed the tags themselves (It seems only css supports tags).
So for now I've wrote this little snippet that checks each element of the page and replace all ${
tag
}
with string from chrome.i18n.getMessage(
tag
)
:
!function()
{
let i18nRegExp = /\$\{([^}]+)\}/g,
cache = {},
i18n = function(a,b)
{
return cache[b] || (cache[b] = chrome.i18n.getMessage(b) || a);
};
!function loop(node)
{
if (node.attributes)
for(let i = 0; i < node.attributes.length; i++)
node.attributes[i].value = node.attributes[i].value.replace(i18nRegExp, i18n);
if (!node.childNodes.length)
node.textContent = node.textContent.replace(i18nRegExp, i18n);
else
for(let i = 0; i < node.childNodes.length; i++)
loop(node.childNodes[i]);
}(document.body.parentNode);
}();