Skip to content

Commit

Permalink
Release 1.5
Browse files Browse the repository at this point in the history
1. 设置中增加“使用共享密钥”开关,您可以选择使用共享密钥或您自己的私钥。个人私钥申请地址:
    https://api.fanyi.baidu.com/product/11
2. 完善报错信息本地化支持。
  • Loading branch information
huaiyinfeilong committed May 22, 2023
1 parent 89294c4 commit 5933848
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 35 deletions.
7 changes: 7 additions & 0 deletions addon/doc/zh_CN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

## 升级日志

### Version 1.5

* 设置中增加“使用共享密钥”开关,您可以选择使用共享密钥或您自己的私钥。个人私钥申请地址:
[https://api.fanyi.baidu.com/product/11](https://api.fanyi.baidu.com/product/11)
* 完善报错信息本地化支持。

### Version 1.4

* 完善帮助文档,修复英文帮助文档中的错误。
Expand All @@ -44,6 +50,7 @@
* 禁用自动翻译:不使用自动翻译功能。

注意:开启自动翻译功能后,NVDA所有朗读内容都会发送至翻译服务器,NVDA朗读响应速度将因翻译结果而有不同程度的减慢。

### Version 1.1

新增反向翻译功能,可从目标语言翻译为源语言。
57 changes: 47 additions & 10 deletions addon/globalPlugins/baiduTranslation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding=utf-8

import api
import globalPluginHandler
import scriptHandler
Expand All @@ -13,6 +15,8 @@

addonHandler.initTranslation()

_translator = BaiduTranslator()


class TranslationSettingsPanel(gui.SettingsPanel):
# Translators: Settings panel title
Expand Down Expand Up @@ -58,6 +62,26 @@ def makeSettings(self, sizer):
choices=self._automatic_translation_list
)
self.automaticTranslationChoice.Select(config.conf["baiduTranslation"]["autoTrans"])
# Translators: The label for using share key checkbox
using_share_key_label = _("Using share key")
self.usingShareKeyCheckBox = helper.addItem(
wx.CheckBox(self, label=_(using_share_key_label))
)
self.usingShareKeyCheckBox.SetValue(config.conf["baiduTranslation"]["usingShareKey"])
# Translators: The label for my APP ID textbox
my_app_id_label = _("My APP ID")
self.myAppIdTextCtrl = helper.addLabeledControl(
_(my_app_id_label),
wx.TextCtrl,
)
self.myAppIdTextCtrl.SetValue(config.conf["baiduTranslation"]["myAppId"])
# Translators: The label for my APP secret textbox
my_app_secret_label = _("My APP secret")
self.myAppSecretTextCtrl = helper.addLabeledControl(
_(my_app_secret_label),
wx.TextCtrl,
)
self.myAppSecretTextCtrl.SetValue(config.conf["baiduTranslation"]["myAppSecret"])

def onSave(self):
config.conf["baiduTranslation"]["autoFromLang"] = self.autoFromLangOption.IsChecked()
Expand All @@ -72,6 +96,14 @@ def onSave(self):
self.targetLanguageChoice.GetSelection()
]
config.conf["baiduTranslation"]["autoTrans"] = self.automaticTranslationChoice.GetSelection()
config.conf["baiduTranslation"]["usingShareKey"] = self.usingShareKeyCheckBox.IsChecked()
config.conf["baiduTranslation"]["myAppId"] = self.myAppIdTextCtrl.GetValue()
config.conf["baiduTranslation"]["myAppSecret"] = self.myAppSecretTextCtrl.GetValue()
appId = config.conf["baiduTranslation"]["shareAppId"] \
if config.conf["baiduTranslation"]["usingShareKey"] else config.conf["baiduTranslation"]["myAppId"]
appSecret = config.conf["baiduTranslation"]["shareAppSecret"] \
if config.conf["baiduTranslation"]["usingShareKey"] else config.conf["baiduTranslation"]["myAppSecret"]
_translator.initialize_translator(appId, appSecret)

# Translators: Category Name
CATEGORY_NAME = _("Baidu Translation")
Expand All @@ -84,14 +116,20 @@ def __init__(self):
"from": "string(default='en')",
"to": "string(default='zh')",
"autoFromLang": "boolean(default=True)",
"autoTrans": "integer(default=0)"
"autoTrans": "integer(default=0)",
"usingShareKey": "boolean(default=True)",
"shareAppId": "string(default='20230423001653246')",
"shareAppSecret": "string(default='dyh8lxcVEZnBhJ8EiEwD')",
"myAppId": "string(default='')",
"myAppSecret": "string(default='')"
}
config.conf.spec["baiduTranslation"] = confspec
gui.settingsDialogs.NVDASettingsDialog.categoryClasses.append(TranslationSettingsPanel)
appid = "20230423001653246"
appsecret = "dyh8lxcVEZnBhJ8EiEwD"
self._translator = BaiduTranslator()
self._translator.initialize_translator(appid, appsecret)
appId = config.conf["baiduTranslation"]["shareAppId"] \
if config.conf["baiduTranslation"]["usingShareKey"] else config.conf["baiduTranslation"]["myAppId"]
appSecret = config.conf["baiduTranslation"]["shareAppSecret"] \
if config.conf["baiduTranslation"]["usingShareKey"] else config.conf["baiduTranslation"]["myAppSecret"]
_translator.initialize_translator(appId, appSecret)
self._speak = speech.speech.speak
speech.speech.speak = self.speak

Expand All @@ -114,7 +152,7 @@ def script_translate(self, gesture):
else:
from_language = config.conf["baiduTranslation"]["from"]
to_language = config.conf["baiduTranslation"]["to"]
self._translator.translate(from_language, to_language, self._data, self._onResult)
_translator.translate(from_language, to_language, self._data, self._onResult)

# Translators: Reverse translate what you just heard
@scriptHandler.script(
Expand All @@ -125,7 +163,7 @@ def script_reverseTranslate(self, gesture):
self._playSound(True)
from_language = config.conf["baiduTranslation"]["from"]
to_language = config.conf["baiduTranslation"]["to"]
self._translator.translate(to_language, from_language, self._data, self._onResult)
_translator.translate(to_language, from_language, self._data, self._onResult)

@scriptHandler.script(
category=CATEGORY_NAME,
Expand Down Expand Up @@ -154,7 +192,6 @@ def script_switchAutomaticTranslationMode(self, gesture):
def script_clipboardTranslation(self, gesture):
self.clipboard_translation()


@scriptHandler.script(
category=CATEGORY_NAME,
# Translators: Reverse translate the content in the clipboard
Expand All @@ -175,7 +212,7 @@ def clipboard_translation(self, reverse=False):
temp = from_language
from_language = to_language
to_language = temp
self._translator.translate(from_language, to_language, text, self._onResult)
_translator.translate(from_language, to_language, text, self._onResult)

def _onResult(self, data):
if data is not None:
Expand All @@ -195,7 +232,7 @@ def speak(self, sequence, *args, **kwargs):
else:
from_language = config.conf["baiduTranslation"]["to"]
to_language = config.conf["baiduTranslation"]["from"]
self._translator.translate(from_language, to_language, self._data, self._onResult)
_translator.translate(from_language, to_language, self._data, self._onResult)
else:
self._speak(sequence, *args, **kwargs)

Expand Down
36 changes: 35 additions & 1 deletion addon/globalPlugins/baiduTranslation/translators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# coding=utf-8

import threading
import urllib.request
import json
import time
import hashlib
import addonHandler

# 初始化翻译引擎
addonHandler.initTranslation()


class BaiduTranslator(object):
Expand Down Expand Up @@ -63,8 +69,36 @@ def run(self):
response = opener.open(request)
data = json.loads(response.read().decode("utf-8"))
if data.get("error_code"):
errorCode = data.get("error_code")
message = ""
errorDescription = {
# Translators: Request timeout
"52001": _("Request timeout"),
# Translators: System error
"52002": _("System error"),
# Translators: Unauthorized user
"52003": _("Unauthorized user"),
# Translators: The required parameter is empty
"54000": _("The required parameter is empty"),
# Translators: Signature error
"54001": _("Signature error"),
# Translators: Access frequency limited
"54003": _("Access frequency limited"),
# Translators: Insufficient account balance
"54004": _("Insufficient account balance"),
# Translators: Long query requests are frequent
"54005": _("Long query requests are frequent"),
# Translators: Illegal client IP
"58000": _("Illegal client IP"),
# Translators: Translation language direction not supported
"58001": _("Translation language direction not supported")
}
message = errorDescription.get(errorCode)
if message is None:
message = f"{data.get('error_msg')}, error code={errorCode}"
# Translators: Translation failed message
result = _("Translation failed:") + data.get("error_msg")
failedMessage = _("Translation failed: ")
result = f"{_(failedMessage)}{_(message)}"
else:
result = "\n".join([r.get("dst") for r in data.get("trans_result")])
self._on_result(result)
Loading

0 comments on commit 5933848

Please sign in to comment.