adding data-localize for automatic localization

This commit is contained in:
Andy Fuchs 2023-03-02 10:24:03 +01:00
commit c7182c619a
No known key found for this signature in database
2 changed files with 30 additions and 60 deletions

View file

@ -15,12 +15,12 @@
<div class="sdpi-wrapper">
<form id="property-inspector">
<div class="sdpi-item">
<div data-localize class="sdpi-item-label">Name</div>
<input class="sdpi-item-value" name="name" type="text" value="" />
<div data-localize class="sdpi-item-label" title="My name">Name</div>
<input data-localize class="sdpi-item-value" name="name" type="text" value="" placeholder="Click Me"/>
</div>
<div type="textarea" class="sdpi-item">
<div class="sdpi-item-label up20" data-localize>Message</div>
<div class="sdpi-item-label up20" data-localize title="My message">Message</div>
<div class="sdpi-item-value textarea">
<textarea
name="message"
@ -30,19 +30,18 @@
></textarea>
</div>
</div>
<div class="sdpi-item">
<div class="sdpi-item-label" data-localize>Button</div>
<button
class="sdpi-item-value"
value="Click Me"
data-localize
id="open-external"
>
Click Me
</button>
</div>
</form>
<div class="sdpi-item">
<div class="sdpi-item-label" data-localize title="My button">Button</div>
<button
class="sdpi-item-value"
value="Click Me"
data-localize
id="open-external"
>
Click Me
</button>
</div>
</div>
<div class="sdpi-info-label hidden" style="top: -1000" value=""></div>

View file

@ -2,22 +2,20 @@
/// <reference path="../../../libs/js/utils.js" />
$PI.onConnected((jsn) => {
$PI.loadLocalization('../../../');
const form = document.querySelector('#property-inspector');
const {actionInfo, appInfo, connection, messageType, port, uuid} = jsn;
const {payload, context} = actionInfo;
const {settings} = payload;
const form = document.querySelector('#property-inspector');
const { actionInfo, appInfo, connection, messageType, port, uuid } = jsn;
const { payload, context } = actionInfo;
const { settings } = payload;
Utils.setFormValue(settings, form);
Utils.setFormValue(settings, form);
form.addEventListener(
'input',
Utils.debounce(150, () => {
const value = Utils.getFormValue(form);
$PI.setSettings(value);
})
);
form.addEventListener(
'input',
Utils.debounce(150, () => {
const value = Utils.getFormValue(form);
$PI.setSettings(value);
})
);
});
/**
@ -25,36 +23,9 @@ $PI.onConnected((jsn) => {
* (this can be removed if the external window is not used)
*/
window.sendToInspector = (data) => {
console.log(data);
console.log(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();
window.open('../../../external.html');
});