mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-22 07:51:00 +00:00
add support for auto language. Not perfect, it picks a language not supported sometimes
This commit is contained in:
parent
9bf3eabb6e
commit
55d3c32b40
3 changed files with 13 additions and 3 deletions
12
app/app.py
12
app/app.py
|
@ -1,6 +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
|
||||
|
||||
def get_remote_address():
|
||||
if request.headers.getlist("X-Forwarded-For"):
|
||||
|
@ -10,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="en", frontend_language_target="es"):
|
||||
def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="auto", frontend_language_target="en"):
|
||||
from app.init import boot
|
||||
boot()
|
||||
|
||||
|
@ -21,7 +22,7 @@ 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]), None)
|
||||
frontend_argos_language_source = next(iter([l for l in languages if l.code == frontend_language_source or l.code == 'auto']), 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:
|
||||
|
@ -187,9 +188,16 @@ 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)
|
||||
|
||||
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:
|
||||
|
|
|
@ -299,6 +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' })
|
||||
if (self.langs.length === 0){
|
||||
self.loading = false;
|
||||
self.error = "No languages available. Did you install the models correctly?"
|
||||
|
|
|
@ -4,3 +4,4 @@ flask-swagger==0.2.14
|
|||
flask-swagger-ui==3.36.0
|
||||
Flask-Limiter==1.4
|
||||
waitress==1.4.4
|
||||
langdetect
|
||||
|
|
Loading…
Reference in a new issue