Only choose from languages available in argos

This commit is contained in:
Piero Toffanin 2021-01-15 12:12:00 -05:00
parent 41e381ae0c
commit db01aeb404
3 changed files with 13 additions and 5 deletions

View file

@ -22,8 +22,7 @@ const res = await fetch("https://libretranslate.com/translate", {
source: "en", source: "en",
target: "es" target: "es"
}), }),
headers: { headers: { "Content-Type": "application/json" }
"Content-Type": "application/json"}
}); });
console.log(await res.json()); console.log(await res.json());

View file

@ -2,6 +2,8 @@ from flask import Flask, render_template, jsonify, request, abort, send_from_dir
from flask_swagger import swagger from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint from flask_swagger_ui import get_swaggerui_blueprint
from langdetect import detect_langs from langdetect import detect_langs
from langdetect import DetectorFactory
DetectorFactory.seed = 0 # deterministic
def get_remote_address(): def get_remote_address():
if request.headers.getlist("X-Forwarded-For"): if request.headers.getlist("X-Forwarded-For"):
@ -18,6 +20,11 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
from app.language import languages from app.language import languages
app = Flask(__name__) app = Flask(__name__)
# For faster access
language_map = {}
for l in languages:
language_map[l.code] = l.name
if debug: if debug:
app.config['TEMPLATES_AUTO_RELOAD'] = True app.config['TEMPLATES_AUTO_RELOAD'] = True
@ -197,11 +204,14 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
q = q[:char_limit] q = q[:char_limit]
if source_lang == 'auto': if source_lang == 'auto':
candidate_langs = detect_langs(q) candidate_langs = list(filter(lambda l: l.lang in language_map, detect_langs(q)))
if len(candidate_langs) > 0: if len(candidate_langs) > 0:
candidate_langs.sort(key=lambda l: l.prob, reverse=True) candidate_langs.sort(key=lambda l: l.prob, reverse=True)
if debug: if debug:
print(candidate_langs) print(candidate_langs)
source_lang = next(iter([l.code for l in languages if l.code == candidate_langs[0].lang]), None) source_lang = next(iter([l.code for l in languages if l.code == candidate_langs[0].lang]), None)
if not source_lang: if not source_lang:
source_lang = 'en' source_lang = 'en'

View file

@ -345,8 +345,7 @@ document.addEventListener('DOMContentLoaded', function(){
' source: "' + this.$options.filters.escape(this.sourceLang) + '",', ' source: "' + this.$options.filters.escape(this.sourceLang) + '",',
' target: "' + this.$options.filters.escape(this.targetLang) + '"', ' target: "' + this.$options.filters.escape(this.targetLang) + '"',
' }),', ' }),',
' headers: {', ' headers: { "Content-Type": "application/json" }',
' "Content-Type": "application/json"}',
' });', ' });',
'', '',
'console.log(await res.json());'].join("\n"); 'console.log(await res.json());'].join("\n");