From eb89369c6f96f855fc468d686cc54ffa7b251190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 26 Jan 2023 10:54:56 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20New=20gettext=20knockout=20binding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows to wrap a value into a gettext call to translate dynamic values (if a translation is available). --- src/octoprint/server/util/flask.py | 1 + src/octoprint/static/js/app/bindings/gettext.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/octoprint/static/js/app/bindings/gettext.js diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index bb1b52c58..799a0f9d9 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -1759,6 +1759,7 @@ def collect_core_assets(preferred_stylesheet="css"): assets["js"] = [ "js/app/bindings/allowbindings.js", "js/app/bindings/contextmenu.js", + "js/app/bindings/gettext.js", "js/app/bindings/invisible.js", "js/app/bindings/popover.js", "js/app/bindings/qrcode.js", diff --git a/src/octoprint/static/js/app/bindings/gettext.js b/src/octoprint/static/js/app/bindings/gettext.js new file mode 100644 index 000000000..5a897a138 --- /dev/null +++ b/src/octoprint/static/js/app/bindings/gettext.js @@ -0,0 +1,16 @@ +ko.bindingHandlers.gettext = { + init: function () { + // Prevent binding on the dynamically-injected text node (as developers are unlikely to expect that, and it has security implications). + // It should also make things faster, as we no longer have to consider whether the text node might be bindable. + return {controlsDescendantBindings: true}; + }, + update: function (element, valueAccessor) { + var gt = + gettext || + function (text) { + return text; + }; + ko.utils.setTextContent(element, gt(valueAccessor())); + } +}; +ko.virtualElements.allowedBindings["gettext"] = true;