From 7b64deeb372ca17a4f13f812df153e2a20510fed Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 6 Sep 2022 18:34:15 +0300 Subject: [PATCH] create templates on demand --- js/localization.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/js/localization.js b/js/localization.js index 24f60d28..0d81b1c5 100644 --- a/js/localization.js +++ b/js/localization.js @@ -19,7 +19,10 @@ function t(key, params, strict = true) { Object.assign(t, { cache: {}, - template: {}, + template: new Proxy({}, { + get: (obj, k, _) => obj[k] || + (_ = $(`template[data-id="${k}"]`)) && (obj[k] = t.createTemplate(_)), + }), ALLOWED_TAGS: ['a', 'b', 'code', 'i', 'sub', 'sup', 'wbr'], RX_WORD_BREAK: new RegExp([ '(', @@ -31,7 +34,7 @@ Object.assign(t, { ')', /(?!\b|\s|$)/, ].map(rx => rx.source || rx).join(''), 'gu'), - SELECTOR: '[i18n], template', + SELECTOR: '[i18n]', HTML(html) { return typeof html !== 'string' @@ -47,10 +50,6 @@ Object.assign(t, { } for (const node of nodes) { if (!node.localName) continue; - if (node.localName === 'template') { - t.createTemplate(node); - continue; - } const attr = node.getAttribute('i18n'); if (!attr) continue; for (const part of attr.split(',')) {