-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9b3a53
commit 18e89f0
Showing
1 changed file
with
0 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,48 +48,3 @@ def api_response(code, body): | |
"multiValueHeaders": {"Access-Control-Allow-Methods": ["POST", "OPTIONS", "GET", "PATCH", "DELETE"]}, | ||
"body": body | ||
} | ||
|
||
|
||
def resp_handler(func=None, headers=None): | ||
def handle(*args, **kwargs): | ||
try: | ||
resp = func(*args, **kwargs) | ||
return api_response(200, resp) | ||
except LookupError: | ||
resp = JsonPayloadBuilder().add_status(False).add_data(None) \ | ||
.add_message("Not found").compile() | ||
return api_response(404, resp) | ||
except Exception as e: | ||
logging.error(str(e)) | ||
resp = JsonPayloadBuilder().add_status(False).add_data(None) \ | ||
.add_message("Internal error, please contact [email protected].").compile() | ||
return api_response(500, resp) | ||
|
||
return handle | ||
|
||
|
||
def translate_text(text): | ||
src_lang = client.detect_language(request={ | ||
"parent": parent, | ||
"content": text, | ||
"mime_type": "text/plain" | ||
}).languages[0].language_code | ||
|
||
translations = {} | ||
|
||
for lang in langs: | ||
if lang == src_lang: | ||
translations[lang] = text | ||
continue | ||
|
||
translated = client.translate_text(request={ | ||
"parent": parent, | ||
"contents": [text], | ||
"mime_type": "text/plain", | ||
"source_language_code": src_lang, | ||
"target_language_code": lang | ||
}).translations[0].translated_text | ||
|
||
translations[lang] = translated or '' | ||
|
||
return src_lang, translations |