Skip to content

Commit

Permalink
<feature> add custom deepl translation
Browse files Browse the repository at this point in the history
update version to v2.5.4
  • Loading branch information
anonymousException committed Sep 7, 2024
1 parent 0e9b425 commit b21e7cd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/custom.txt
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"}}
46 changes: 46 additions & 0 deletions src/custom_engine/deepl_custom.py
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)
1 change: 0 additions & 1 deletion src/supported_language/deepl.source.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Auto Detect:AUTO
Bulgarian:BG
Czech:CS
Danish:DA
Expand Down

0 comments on commit b21e7cd

Please sign in to comment.