Merge pull request #135 from dingedi/feature/add-html-translation

Feature/add html translation
This commit is contained in:
Piero Toffanin 2021-09-11 16:03:18 -04:00 committed by GitHub
commit 678c9fb6e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 7 deletions

View file

@ -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: '<a href="#" class="green">Hello!</a>',
source: "en",
target: "es",
format: "html"
}),
headers: { "Content-Type": "application/json" }
});
console.log(await res.json());
```
Response:
```javascript
{
"translatedText": "<a href\"#='class=\"green\"'>¡Hola!</a>"
}
```
## 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.

View file

@ -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:

View file

@ -11,3 +11,4 @@ morfessor==2.0.6
polyglot==16.7.4
appdirs==1.4.4
APScheduler==3.7.0
translatehtml==1.5.1