mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-05 07:20:13 +01:00
Merge pull request #135 from dingedi/feature/add-html-translation
Feature/add html translation
This commit is contained in:
commit
678c9fb6e2
31
README.md
31
README.md
@ -12,6 +12,9 @@ Free and Open Source Machine Translation API, entirely self-hosted. Unlike other
|
|||||||
|
|
||||||
## API Examples
|
## API Examples
|
||||||
|
|
||||||
|
|
||||||
|
### Plain Text
|
||||||
|
|
||||||
Request:
|
Request:
|
||||||
|
|
||||||
```javascript
|
```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
|
## Install and Run
|
||||||
|
|
||||||
You can run your own API server in just a few lines of setup!
|
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)'
|
gunicorn --bind 0.0.0.0:5000 'wsgi:app(api_keys=True)'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Manage API Keys
|
## 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.
|
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.
|
||||||
|
41
app/app.py
41
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,18 @@ 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
|
||||||
|
enum: [text, html]
|
||||||
|
default: text
|
||||||
|
example: text
|
||||||
|
required: false
|
||||||
|
description: >
|
||||||
|
Format of source text:
|
||||||
|
* `text` - Plain text
|
||||||
|
* `html` - HTML markup
|
||||||
- in: formData
|
- in: formData
|
||||||
name: api_key
|
name: api_key
|
||||||
schema:
|
schema:
|
||||||
@ -323,10 +336,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 +411,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 +437,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:
|
||||||
|
@ -11,3 +11,4 @@ morfessor==2.0.6
|
|||||||
polyglot==16.7.4
|
polyglot==16.7.4
|
||||||
appdirs==1.4.4
|
appdirs==1.4.4
|
||||||
APScheduler==3.7.0
|
APScheduler==3.7.0
|
||||||
|
translatehtml==1.5.1
|
Loading…
Reference in New Issue
Block a user