From b73e146baf828233f0d0e5dc4d9b813f4e5ac452 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 24 Sep 2021 11:30:19 -0400 Subject: [PATCH] Fix HTML translation formatting, automatically detect HTML in translation UI --- app/app.py | 4 ++-- app/templates/index.html | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/app.py b/app/app.py index 8576996..ea54a2d 100644 --- a/app/app.py +++ b/app/app.py @@ -425,7 +425,7 @@ def create_app(args): translator = src_langs[idx].get_translation(tgt_lang) if text_format == "html": - translated_text = str(translate_html(translator, transliterate(text, target_lang=source_langs[idx]))) + translated_text = str(translate_html(translator, text)) else: translated_text = translator.translate(transliterate(text, target_lang=source_langs[idx])) @@ -439,7 +439,7 @@ def create_app(args): translator = src_langs[0].get_translation(tgt_lang) if text_format == "html": - translated_text = str(translate_html(translator, transliterate(q, target_lang=source_langs[0]))) + translated_text = str(translate_html(translator, q)) else: translated_text = translator.translate(transliterate(q, target_lang=source_langs[0])) return jsonify( diff --git a/app/templates/index.html b/app/templates/index.html index fe5ac16..327c592 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -241,7 +241,7 @@ // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0 // API host/endpoint var BaseUrl = window.location.protocol + "//" + window.location.host; - + var htmlRegex = /<(.*)>.*?|<(.*)\/>/; document.addEventListener('DOMContentLoaded', function(){ var sidenavElems = document.querySelectorAll('.sidenav'); var sidenavInstances = M.Sidenav.init(sidenavElems); @@ -366,12 +366,17 @@ ' body: JSON.stringify({', ' q: "' + this.$options.filters.escape(this.inputText) + '",', ' source: "' + this.$options.filters.escape(this.sourceLang) + '",', - ' target: "' + this.$options.filters.escape(this.targetLang) + '"', + ' target: "' + this.$options.filters.escape(this.targetLang) + '",', + ' format: "' + (this.isHtml ? "html" : "text") + '"', ' }),', ' headers: { "Content-Type": "application/json" }', '});', '', 'console.log(await res.json());'].join("\n"); + }, + + isHtml: function(){ + return htmlRegex.test(this.inputText); } }, filters: { @@ -425,6 +430,7 @@ data.append("q", self.inputText); data.append("source", self.sourceLang); data.append("target", self.targetLang); + data.append("format", self.isHtml ? "html" : "text"); data.append("api_key", localStorage.getItem("api_key") || ""); request.open('POST', BaseUrl + '/translate', true);