From 0a7c8ebae975586ad9e500b0db9c9cc85bfc5984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Thuret?= Date: Sat, 11 Sep 2021 15:08:57 +0200 Subject: [PATCH] Add html translation --- app/app.py | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/app/app.py b/app/app.py index df00f76..19bb846 100644 --- a/app/app.py +++ b/app/app.py @@ -10,6 +10,7 @@ from app.language import detect_languages, transliterate from .api_keys import Database +from translatehtml import translate_html def get_json_dict(request): d = request.get_json() @@ -262,6 +263,13 @@ def create_app(args): example: es required: true 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 name: api_key schema: @@ -323,10 +331,12 @@ def create_app(args): q = json.get("q") source_lang = json.get("source") target_lang = json.get("target") + text_format = json.get("format") else: q = request.values.get("q") source_lang = request.values.get("source") target_lang = request.values.get("target") + text_format = request.values.get("format") if not q: abort(400, description="Invalid request: missing q parameter") @@ -396,14 +406,25 @@ def create_app(args): if tgt_lang is None: 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: if batch: results = [] for idx, text in enumerate(q): 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( { "translatedText": results @@ -411,11 +432,14 @@ def create_app(args): ) else: 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( { - "translatedText": translator.translate( - transliterate(q, target_lang=source_langs[0]) - ) + "translatedText": translated_text } ) except Exception as e: