From 30b3382af8d345339182917a4195ea02a503fba1 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 9 Dec 2022 14:28:14 -0500 Subject: [PATCH 1/7] Fix CSS indent on firefox --- app/static/css/main.css | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/static/css/main.css b/app/static/css/main.css index 9a0ce4b..7ee8554 100644 --- a/app/static/css/main.css +++ b/app/static/css/main.css @@ -61,14 +61,6 @@ h3.header { position: relative; } -@-moz-document url-prefix() { - .language-select select { - -moz-appearance: none; - text-indent: -2px; - margin-right: -8px; - } -} - .language-select:after { content: ""; width: 0.5em; From b87b210bfc93e20eaad15f296098fef94ca7549b Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 9 Dec 2022 14:29:44 -0500 Subject: [PATCH 2/7] Fix indent --- app/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app.py b/app/app.py index 36e1ad3..fc13958 100644 --- a/app/app.py +++ b/app/app.py @@ -517,7 +517,7 @@ def create_app(args): return jsonify( { "translatedText": unescape(translated_text), - "detectedLanguage": source_langs[0] + "detectedLanguage": source_langs[0] } ) else: From 0478d4ee2cf66f7400a00456dafe946a4690b075 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 9 Dec 2022 14:35:39 -0500 Subject: [PATCH 3/7] Handle empty translation --- app/app.py | 4 ++-- app/language.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/app.py b/app/app.py index fc13958..a25477b 100644 --- a/app/app.py +++ b/app/app.py @@ -501,9 +501,9 @@ def create_app(args): ) else: return jsonify( - { + { "translatedText": results - } + } ) else: translator = src_langs[0].get_translation(tgt_lang) diff --git a/app/language.py b/app/language.py index a19ad2f..3850722 100644 --- a/app/language.py +++ b/app/language.py @@ -81,9 +81,9 @@ def detect_languages(text): def improve_translation_formatting(source, translation, improve_punctuation=True): source = source.strip() - if not len(source): + if not len(source) or not len(translation): return "" - + if improve_punctuation: source_last_char = source[len(source) - 1] translation_last_char = translation[len(translation) - 1] From e79089b5c15d1c0ca499b22637d7baea58e3bca2 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 9 Dec 2022 16:36:12 -0500 Subject: [PATCH 4/7] Extend /languages to include targets --- app/app.py | 16 +++++----------- app/language.py | 5 ++++- app/static/js/app.js | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/app.py b/app/app.py index a25477b..70b5677 100644 --- a/app/app.py +++ b/app/app.py @@ -112,6 +112,9 @@ def create_app(args): if not args.disable_files_translation: remove_translated_files.setup(get_upload_dir()) languages = load_languages() + language_pairs = {} + for lang in languages: + language_pairs[lang.code] = [l.to_lang.code for l in lang.translations_from] # Map userdefined frontend languages to argos language object. if args.frontend_language_source == "auto": @@ -269,17 +272,8 @@ def create_app(args): name: type: string description: Human-readable language name (in English) - 429: - description: Slow down - schema: - id: error-slow-down - type: object - properties: - error: - type: string - description: Reason for slow down """ - return jsonify([{"code": l.code, "name": l.name} for l in languages]) + return jsonify([{"code": l.code, "name": l.name, "targets": [language_pairs.get(l.code, [])]} for l in languages]) # Add cors @app.after_request @@ -940,7 +934,7 @@ def create_app(args): return jsonify({"success": True}) swag = swagger(app) - swag["info"]["version"] = "1.3.0" + swag["info"]["version"] = "1.3.1" swag["info"]["title"] = "LibreTranslate" @app.route("/spec") diff --git a/app/language.py b/app/language.py index 3850722..2e358c2 100644 --- a/app/language.py +++ b/app/language.py @@ -81,9 +81,12 @@ def detect_languages(text): def improve_translation_formatting(source, translation, improve_punctuation=True): source = source.strip() - if not len(source) or not len(translation): + if not len(source): return "" + if not len(translation): + return source + if improve_punctuation: source_last_char = source[len(source) - 1] translation_last_char = translation[len(translation) - 1] diff --git a/app/static/js/app.js b/app/static/js/app.js index cd53e0e..d523359 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -141,7 +141,7 @@ document.addEventListener('DOMContentLoaded', function(){ isHtml: function(){ return htmlRegex.test(this.inputText); }, - canSendSuggestion() { + canSendSuggestion: function(){ return this.translatedText.trim() !== "" && this.translatedText !== this.savedTanslatedText; } }, From 6621c51b52947085f31f63d04decf58ec3583c76 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 9 Dec 2022 16:38:58 -0500 Subject: [PATCH 5/7] Fix array --- app/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app.py b/app/app.py index 70b5677..aef8242 100644 --- a/app/app.py +++ b/app/app.py @@ -273,7 +273,7 @@ def create_app(args): type: string description: Human-readable language name (in English) """ - return jsonify([{"code": l.code, "name": l.name, "targets": [language_pairs.get(l.code, [])]} for l in languages]) + return jsonify([{"code": l.code, "name": l.name, "targets": language_pairs.get(l.code, [])} for l in languages]) # Add cors @app.after_request From 0875fdc4330142e27e735534a50bbbb001a430e1 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sat, 10 Dec 2022 00:03:21 -0500 Subject: [PATCH 6/7] Server-side check for target lang --- app/app.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/app.py b/app/app.py index aef8242..40d0095 100644 --- a/app/app.py +++ b/app/app.py @@ -114,7 +114,7 @@ def create_app(args): languages = load_languages() language_pairs = {} for lang in languages: - language_pairs[lang.code] = [l.to_lang.code for l in lang.translations_from] + language_pairs[lang.code] = sorted([l.to_lang.code for l in lang.translations_from]) # Map userdefined frontend languages to argos language object. if args.frontend_language_source == "auto": @@ -480,6 +480,9 @@ def create_app(args): results = [] for idx, text in enumerate(q): translator = src_langs[idx].get_translation(tgt_lang) + if translator is None: + abort(400, description="%s (%s) is not available as a target language from %s (%s)" % (tgt_lang.name, tgt_lang.code, src_langs[idx].name, src_langs[idx].code)) + if text_format == "html": translated_text = str(translate_html(translator, text)) else: @@ -501,6 +504,8 @@ def create_app(args): ) else: translator = src_langs[0].get_translation(tgt_lang) + if translator is None: + abort(400, description="%s (%s) is not available as a target language from %s (%s)" % (tgt_lang.name, tgt_lang.code, src_langs[0].name, src_langs[0].code)) if text_format == "html": translated_text = str(translate_html(translator, q)) From 2b0074b94f46689b40a298c427105e325f7c979c Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sat, 10 Dec 2022 00:21:38 -0500 Subject: [PATCH 7/7] Only show target languages, check swap, bump version --- VERSION | 2 +- app/app.py | 5 +++++ app/static/js/app.js | 16 +++++++++++++++- app/templates/index.html | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 1892b92..31e5c84 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.2 +1.3.3 diff --git a/app/app.py b/app/app.py index 40d0095..9976ac0 100644 --- a/app/app.py +++ b/app/app.py @@ -272,6 +272,11 @@ def create_app(args): name: type: string description: Human-readable language name (in English) + targets: + type: array + items: + type: string + description: Supported target language codes """ return jsonify([{"code": l.code, "name": l.name, "targets": language_pairs.get(l.code, [])} for l in languages]) diff --git a/app/static/js/app.js b/app/static/js/app.js index d523359..d32b140 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -143,6 +143,14 @@ document.addEventListener('DOMContentLoaded', function(){ }, canSendSuggestion: function(){ return this.translatedText.trim() !== "" && this.translatedText !== this.savedTanslatedText; + }, + targetLangs: function(){ + if (!this.sourceLang) return this.langs; + else{ + var lang = this.langs.find(l => l.code === this.sourceLang); + if (!lang) return this.langs; + return lang.targets.map(t => this.langs.find(l => l.code === t)); + } } }, filters: { @@ -161,7 +169,13 @@ document.addEventListener('DOMContentLoaded', function(){ } }, swapLangs: function(e){ - this.closeSuggestTranslation(e) + this.closeSuggestTranslation(e); + + // Make sure that we can swap + // by checking that the current target language + // has source language as target + var tgtLang = this.langs.find(l => l.code === this.targetLang); + if (tgtLang.targets.indexOf(this.sourceLang) === -1) return; // Not supported var t = this.sourceLang; this.sourceLang = this.targetLang; diff --git a/app/templates/index.html b/app/templates/index.html index e9068ea..396754f 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -156,7 +156,7 @@ Translate into