-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<feature> add custom deepl translation
update version to v2.5.4
- Loading branch information
1 parent
0e9b425
commit b21e7cd
Showing
3 changed files
with
47 additions
and
2 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 |
---|---|---|
@@ -1 +1 @@ | ||
{"Caiyun(Token Required)": {"url": "https://fanyi.caiyunapp.com/", "key_edit": true, "secret_edit": false, "is_queue": true, "target": "caiyun.target.rst", "source": "caiyun.source.rst", "file_name": "caiyun.py"}, "Baidu(Token Required)": {"url": "https://fanyi-api.baidu.com/", "key_edit": true, "secret_edit": true, "is_queue": false, "target": "baidu.target.rst", "source": "baidu.source.rst", "file_name": "baidu.py"}} | ||
{"Caiyun(Token Required)": {"url": "https://fanyi.caiyunapp.com/", "key_edit": true, "secret_edit": false, "is_queue": true, "target": "caiyun.target.rst", "source": "caiyun.source.rst", "file_name": "caiyun.py"}, "Baidu(Token Required)": {"url": "https://fanyi-api.baidu.com/", "key_edit": true, "secret_edit": true, "is_queue": false, "target": "baidu.target.rst", "source": "baidu.source.rst", "file_name": "baidu.py"}, "DeepL Custom(Token Required)": {"url": "https://www.deepl.com/account/?utm_source=github&utm_medium=github-python-readme", "key_edit": true, "secret_edit": false, "is_queue": true, "target": "deepl.target.rst", "source": "deepl.source.rst", "file_name": "deepl_custom.py"}} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# if your api can not input untranslated list such as ['Hello','World'] | ||
# but can only accept single text input like 'Hello World' | ||
# use this api , the example is baidu.py | ||
|
||
|
||
def tranlate_single(app_key, app_secret, source, target, proxies, text): | ||
# return translated_text | ||
pass | ||
|
||
|
||
# otherwise,use this api | ||
# notice that, any other function call should not be out of this function | ||
# you can directly include other function in this function ,like tranlate_deepl , otherwise will cause NameError | ||
def tranlate_queue(app_key, app_secret, source, target, proxies, q): | ||
# write import inside the function , otherwise will cause NameError | ||
import json | ||
import requests | ||
import traceback | ||
import deepl | ||
class TranslateResponse: | ||
def __init__(self, res): | ||
self.detected_source_lang = res.detected_source_lang | ||
self.translatedText = res.text | ||
|
||
def tranlate_deepl(token, source, target, proxies, q): | ||
translator = deepl.Translator(token, proxy=proxies) | ||
result = translator.translate_text(q, target_lang=target, source_lang=source) | ||
l = [] | ||
for i, e in enumerate(result): | ||
translateResponse = TranslateResponse(e) | ||
dic = dict() | ||
dic['untranslatedText'] = q[i] | ||
dic['translatedText'] = translateResponse.translatedText | ||
l.append(dic) | ||
# you are supposed to return a list | ||
# the list item should be a dict which contains two keys | ||
# 'untranslatedText' : the original text | ||
# 'translatedText : the translated text | ||
return l | ||
|
||
return tranlate_deepl(app_key, source, target, proxies, q) | ||
|
||
|
||
# source = ["Deepl is the best translation service.", "How are you"] | ||
# result = tranlate_queue('app_key', None, 'EN', 'ZH', None, source) | ||
# print(result) |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
Auto Detect:AUTO | ||
Bulgarian:BG | ||
Czech:CS | ||
Danish:DA | ||
|