forked from mirrors/LibreTranslate
Add html translation
This commit is contained in:
parent
6da55cc52d
commit
0a7c8ebae9
1 changed files with 30 additions and 6 deletions
36
app/app.py
36
app/app.py
|
@ -10,6 +10,7 @@ from app.language import detect_languages, transliterate
|
||||||
|
|
||||||
from .api_keys import Database
|
from .api_keys import Database
|
||||||
|
|
||||||
|
from translatehtml import translate_html
|
||||||
|
|
||||||
def get_json_dict(request):
|
def get_json_dict(request):
|
||||||
d = request.get_json()
|
d = request.get_json()
|
||||||
|
@ -262,6 +263,13 @@ def create_app(args):
|
||||||
example: es
|
example: es
|
||||||
required: true
|
required: true
|
||||||
description: Target language code
|
description: Target language code
|
||||||
|
- in: formData
|
||||||
|
name: format
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: html or text
|
||||||
|
required: false
|
||||||
|
description: The format of the source text
|
||||||
- in: formData
|
- in: formData
|
||||||
name: api_key
|
name: api_key
|
||||||
schema:
|
schema:
|
||||||
|
@ -323,10 +331,12 @@ def create_app(args):
|
||||||
q = json.get("q")
|
q = json.get("q")
|
||||||
source_lang = json.get("source")
|
source_lang = json.get("source")
|
||||||
target_lang = json.get("target")
|
target_lang = json.get("target")
|
||||||
|
text_format = json.get("format")
|
||||||
else:
|
else:
|
||||||
q = request.values.get("q")
|
q = request.values.get("q")
|
||||||
source_lang = request.values.get("source")
|
source_lang = request.values.get("source")
|
||||||
target_lang = request.values.get("target")
|
target_lang = request.values.get("target")
|
||||||
|
text_format = request.values.get("format")
|
||||||
|
|
||||||
if not q:
|
if not q:
|
||||||
abort(400, description="Invalid request: missing q parameter")
|
abort(400, description="Invalid request: missing q parameter")
|
||||||
|
@ -396,14 +406,25 @@ def create_app(args):
|
||||||
if tgt_lang is None:
|
if tgt_lang is None:
|
||||||
abort(400, description="%s is not supported" % target_lang)
|
abort(400, description="%s is not supported" % target_lang)
|
||||||
|
|
||||||
|
if not text_format:
|
||||||
|
text_format = "text"
|
||||||
|
|
||||||
|
if text_format not in ["text", "html"]:
|
||||||
|
abort(400, description="%s format is not supported" % text_format)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if batch:
|
if batch:
|
||||||
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)
|
||||||
results.append(translator.translate(
|
|
||||||
transliterate(text, target_lang=source_langs[idx])
|
if text_format == "html":
|
||||||
))
|
translated_text = str(translate_html(translator, transliterate(text, target_lang=source_langs[idx])))
|
||||||
|
else:
|
||||||
|
translated_text = translator.translate(transliterate(text, target_lang=source_langs[idx]))
|
||||||
|
|
||||||
|
results.append(translated_text)
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"translatedText": results
|
"translatedText": results
|
||||||
|
@ -411,11 +432,14 @@ def create_app(args):
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
translator = src_langs[0].get_translation(tgt_lang)
|
translator = src_langs[0].get_translation(tgt_lang)
|
||||||
|
|
||||||
|
if text_format == "html":
|
||||||
|
translated_text = str(translate_html(translator, transliterate(q, target_lang=source_langs[0])))
|
||||||
|
else:
|
||||||
|
translated_text = translator.translate(transliterate(q, target_lang=source_langs[0]))
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"translatedText": translator.translate(
|
"translatedText": translated_text
|
||||||
transliterate(q, target_lang=source_langs[0])
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in a new issue