From bde5dc98ad96546b59e91f5a6e552bf80cbf48e4 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Thu, 24 Aug 2023 14:42:12 +0200 Subject: translations and error handling --- resources/js/helpers.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'resources/js/helpers.js') diff --git a/resources/js/helpers.js b/resources/js/helpers.js index adf0bb7..58e2113 100644 --- a/resources/js/helpers.js +++ b/resources/js/helpers.js @@ -59,9 +59,41 @@ function randomString () { /** * @param {string} msgid + * + * @returns {string} */ -function translate (msgid) { - return DB.translations[Memory.state.Settings.language][msgid]; +function translate (msgid, showTranslationMissing = false) { + let translation = DB.translations[Memory.state.Settings.language][msgid]; + + if (!translation && showTranslationMissing) { + translation = (translate('translation_missing') || 'translation_missing') + `: ${msgid}`; + } + + return translation; +} + +function applyTranslation () { + document.querySelectorAll('body, template').forEach((element) => { + let parentNode = element; + if (element.content) { + parentNode = element.content; + } + + parentNode.querySelectorAll('[data-i18n-msgid]').forEach((node) => { + node.innerHTML = translate(node.dataset.i18nMsgid, true); + }); + + parentNode.querySelectorAll('[data-i18n-properties]').forEach((node) => { + const properties = node.dataset.i18nProperties.split(',') + const msgids = node.dataset.i18nPropertyMsgids.split(',') + for (const idx in properties) { + const property = properties[idx]; + const msgid = msgids[idx]; + + node.setAttribute(property, translate(msgid, true)) + } + }); + }); } /** -- cgit v1.2.3