1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-12-24 10:06:43 +02:00

Misc fixes, debug messages

This commit is contained in:
Piero Toffanin 2021-01-15 12:01:16 -05:00
parent 06b3c12ff6
commit 41e381ae0c
3 changed files with 26 additions and 10 deletions

View File

@ -1,7 +1,7 @@
from flask import Flask, render_template, jsonify, request, abort, send_from_directory
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint
from langdetect import detect
from langdetect import detect_langs
def get_remote_address():
if request.headers.getlist("X-Forwarded-For"):
@ -11,7 +11,7 @@ def get_remote_address():
return ip
def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="auto", frontend_language_target="en"):
def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="en", frontend_language_target="en"):
from app.init import boot
boot()
@ -22,8 +22,16 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
app.config['TEMPLATES_AUTO_RELOAD'] = True
# Map userdefined frontend languages to argos language object.
frontend_argos_language_source = next(iter([l for l in languages if l.code == frontend_language_source or l.code == 'auto']), None)
if frontend_language_source == "auto":
frontend_argos_language_source = type('obj', (object,), {
'code': 'auto',
'name': 'Auto Detect'
})
else:
frontend_argos_language_source = next(iter([l for l in languages if l.code == frontend_language_source]), None)
frontend_argos_language_target = next(iter([l for l in languages if l.code == frontend_language_target]), None)
# Raise AttributeError to prevent app startup if user input is not valid.
if frontend_argos_language_source is None:
raise AttributeError(f"{frontend_language_source} as frontend source language is not supported.")
@ -188,16 +196,24 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la
if char_limit != -1:
q = q[:char_limit]
original_source_lang = source_lang
if source_lang == 'auto':
source_lang = detect(q)
candidate_langs = detect_langs(q)
if len(candidate_langs) > 0:
candidate_langs.sort(key=lambda l: l.prob, reverse=True)
if debug:
print(candidate_langs)
source_lang = next(iter([l.code for l in languages if l.code == candidate_langs[0].lang]), None)
if not source_lang:
source_lang = 'en'
else:
source_lang = 'en'
if debug:
print("Auto detected: %s" % source_lang)
src_lang = next(iter([l for l in languages if l.code == source_lang]), None)
tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None)
if src_lang is None and original_source_lang == 'auto':
return jsonify({"translatedText": "Detected language not supported (" + source_lang + ")" })
if src_lang is None:
abort(400, description="%s is not supported" % source_lang)
if tgt_lang is None:

View File

@ -299,7 +299,7 @@ document.addEventListener('DOMContentLoaded', function(){
if (this.status >= 200 && this.status < 400) {
// Success!
self.langs = JSON.parse(this.response);
self.langs.push({ name: 'Auto (experimental)', code: 'auto' })
self.langs.push({ name: 'Auto Detect (Experimental)', code: 'auto' })
if (self.langs.length === 0){
self.loading = false;
self.error = "No languages available. Did you install the models correctly?"

View File

@ -4,4 +4,4 @@ flask-swagger==0.2.14
flask-swagger-ui==3.36.0
Flask-Limiter==1.4
waitress==1.4.4
langdetect
langdetect==1.0.8