diff --git a/README.md b/README.md index daad3d7..2883ab4 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ Free and Open Source Machine Translation API, entirely self-hosted. Unlike other ## API Examples + +### Plain Text + Request: ```javascript @@ -36,6 +39,33 @@ Response: } ``` +### HTML + +Request: + +```javascript +const res = await fetch("https://libretranslate.com/translate", { + method: "POST", + body: JSON.stringify({ + q: 'Hello!', + source: "en", + target: "es", + format: "html" + }), + headers: { "Content-Type": "application/json" } +}); + +console.log(await res.json()); +``` + +Response: + +```javascript +{ + "translatedText": "¡Hola!" +} +``` + ## Install and Run You can run your own API server in just a few lines of setup! @@ -136,7 +166,6 @@ You can pass application arguments directly to Gunicorn via: gunicorn --bind 0.0.0.0:5000 'wsgi:app(api_keys=True)' ``` - ## Manage API Keys LibreTranslate supports per-user limit quotas, e.g. you can issue API keys to users so that they can enjoy higher requests limits per minute (if you also set `--req-limit`). By default all users are rate-limited based on `--req-limit`, but passing an optional `api_key` parameter to the REST endpoints allows a user to enjoy higher request limits. diff --git a/app/app.py b/app/app.py index df00f76..8576996 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,18 @@ def create_app(args): example: es required: true description: Target language code + - in: formData + name: format + schema: + type: string + enum: [text, html] + default: text + example: text + required: false + description: > + Format of source text: + * `text` - Plain text + * `html` - HTML markup - in: formData name: api_key schema: @@ -323,10 +336,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 +411,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 +437,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: diff --git a/requirements.txt b/requirements.txt index 474456c..da91689 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ morfessor==2.0.6 polyglot==16.7.4 appdirs==1.4.4 APScheduler==3.7.0 +translatehtml==1.5.1 \ No newline at end of file