adding localization script

This commit is contained in:
Andy Fuchs 2023-01-31 13:13:05 +01:00
commit 51486fdabe
No known key found for this signature in database

View file

@ -31,3 +31,30 @@ window.sendToInspector = (data) => {
document.querySelector('#open-external').addEventListener('click', () => {
window.open('../../../external.html');
});
const localize = (s) => {
if(typeof s === 'undefined') return '';
let str = String(s);
try {
str = $localizedStrings[str] || str;
} catch(b) {}
return str;
};
const localizeUI = () => {
const el = document.querySelector('.sdpi-wrapper');
Array.from(el.querySelectorAll('.sdpi-item-label')).forEach(e => {
const s = e.innerText.trim();
e.innerHTML = e.innerHTML.replace(s, localize(s));
});
Array.from(el.querySelectorAll('*:not(script)')).forEach(e => {
if(e.childNodes && e.childNodes.length > 0 && e.childNodes[0].nodeValue && typeof e.childNodes[0].nodeValue === 'string') {
const s = e.childNodes[0].nodeValue.trim();
e.childNodes[0].nodeValue = e.childNodes[0].nodeValue.replace(s, localize(s));
}
});
};
$PI.on('localizationLoaded', (jsn) => {
localizeUI();
});