Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
LibreTranslate
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Riße, Matthias
LibreTranslate
Commits
0a7c8eba
Commit
0a7c8eba
authored
3 years ago
by
Sébastien Thuret
Browse files
Options
Downloads
Patches
Plain Diff
Add html translation
parent
6da55cc5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/app.py
+30
-6
30 additions, 6 deletions
app/app.py
with
30 additions
and
6 deletions
app/app.py
+
30
−
6
View file @
0a7c8eba
...
...
@@ -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
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment