1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-12-18 08:27:03 +02:00

Merge pull request #358 from pierotofy/misc

Miscellaneous Fixes and Improvements
This commit is contained in:
Piero Toffanin 2022-12-10 00:37:41 -05:00 committed by GitHub
commit 8be739a783
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 27 deletions

View File

@ -1 +1 @@
1.3.2 1.3.3

View File

@ -112,6 +112,9 @@ def create_app(args):
if not args.disable_files_translation: if not args.disable_files_translation:
remove_translated_files.setup(get_upload_dir()) remove_translated_files.setup(get_upload_dir())
languages = load_languages() languages = load_languages()
language_pairs = {}
for lang in languages:
language_pairs[lang.code] = sorted([l.to_lang.code for l in lang.translations_from])
# Map userdefined frontend languages to argos language object. # Map userdefined frontend languages to argos language object.
if args.frontend_language_source == "auto": if args.frontend_language_source == "auto":
@ -269,17 +272,13 @@ def create_app(args):
name: name:
type: string type: string
description: Human-readable language name (in English) description: Human-readable language name (in English)
429: targets:
description: Slow down type: array
schema: items:
id: error-slow-down type: string
type: object description: Supported target language codes
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 # Add cors
@app.after_request @app.after_request
@ -486,6 +485,9 @@ def create_app(args):
results = [] results = []
for idx, text in enumerate(q): for idx, text in enumerate(q):
translator = src_langs[idx].get_translation(tgt_lang) 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": if text_format == "html":
translated_text = str(translate_html(translator, text)) translated_text = str(translate_html(translator, text))
else: else:
@ -501,12 +503,14 @@ def create_app(args):
) )
else: else:
return jsonify( return jsonify(
{ {
"translatedText": results "translatedText": results
} }
) )
else: else:
translator = src_langs[0].get_translation(tgt_lang) 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": if text_format == "html":
translated_text = str(translate_html(translator, q)) translated_text = str(translate_html(translator, q))
@ -517,7 +521,7 @@ def create_app(args):
return jsonify( return jsonify(
{ {
"translatedText": unescape(translated_text), "translatedText": unescape(translated_text),
"detectedLanguage": source_langs[0] "detectedLanguage": source_langs[0]
} }
) )
else: else:
@ -940,7 +944,7 @@ def create_app(args):
return jsonify({"success": True}) return jsonify({"success": True})
swag = swagger(app) swag = swagger(app)
swag["info"]["version"] = "1.3.0" swag["info"]["version"] = "1.3.1"
swag["info"]["title"] = "LibreTranslate" swag["info"]["title"] = "LibreTranslate"
@app.route("/spec") @app.route("/spec")

View File

@ -83,7 +83,10 @@ def improve_translation_formatting(source, translation, improve_punctuation=True
if not len(source): if not len(source):
return "" return ""
if not len(translation):
return source
if improve_punctuation: if improve_punctuation:
source_last_char = source[len(source) - 1] source_last_char = source[len(source) - 1]
translation_last_char = translation[len(translation) - 1] translation_last_char = translation[len(translation) - 1]

View File

@ -61,14 +61,6 @@ h3.header {
position: relative; position: relative;
} }
@-moz-document url-prefix() {
.language-select select {
-moz-appearance: none;
text-indent: -2px;
margin-right: -8px;
}
}
.language-select:after { .language-select:after {
content: ""; content: "";
width: 0.5em; width: 0.5em;

View File

@ -141,8 +141,16 @@ document.addEventListener('DOMContentLoaded', function(){
isHtml: function(){ isHtml: function(){
return htmlRegex.test(this.inputText); return htmlRegex.test(this.inputText);
}, },
canSendSuggestion() { canSendSuggestion: function(){
return this.translatedText.trim() !== "" && this.translatedText !== this.savedTanslatedText; 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: { filters: {
@ -161,7 +169,13 @@ document.addEventListener('DOMContentLoaded', function(){
} }
}, },
swapLangs: function(e){ 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; var t = this.sourceLang;
this.sourceLang = this.targetLang; this.sourceLang = this.targetLang;

View File

@ -156,7 +156,7 @@
</a> </a>
<span>Translate into</span> <span>Translate into</span>
<select class="browser-default" v-model="targetLang" ref="targetLangDropdown" @change="handleInput"> <select class="browser-default" v-model="targetLang" ref="targetLangDropdown" @change="handleInput">
<template v-for="option in langs"> <template v-for="option in targetLangs">
<option v-if="option.code !== 'auto'" :value="option.code">[[ option.name ]]</option> <option v-if="option.code !== 'auto'" :value="option.code">[[ option.name ]]</option>
</template> </template>
</select> </select>