Skip to content

Commit

Permalink
Merge pull request #166 from Cheuring/master
Browse files Browse the repository at this point in the history
userAHK 增加免费有道翻译
  • Loading branch information
wo52616111 authored Nov 1, 2024
2 parents 38f83e8 + e2b5d15 commit eef0e02
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
35 changes: 35 additions & 0 deletions userAHK/main.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,38 @@
keyFunc_example1(){
msgbox, example1
}

; end demo


#include translate.ahk


; This function calls the Youdao translation web API to achieve free translation functionality.
; It is an unconventional method, but I hope it can give you some inspiration.
; How to use:
; 1. add below setting under the [Keys] section in `CapsLock+settings.ini`:
; caps_f9=keyFunc_translate_cus
; 2. Save, reload Capslock+ (CapsLock+F5)
; 3. Press `CapsLock+F9` to invoke the function
keyFunc_translate_cus(){
global
selText:=getSelText()
if(selText)
{
ydTranslate_cus(selText)
}
else
{
ClipboardOld:=ClipboardAll
Clipboard:=""
SendInput, ^{Left}^+{Right}^{insert}
ClipWait, 0.05
selText:=Clipboard
ydTranslate_cus(selText)
Clipboard:=ClipboardOld
}
SetTimer, setTransGuiActive, -400
Return

}
106 changes: 106 additions & 0 deletions userAHK/translate.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
ydTranslate_cus(ss)
{
ss:=RegExReplace(ss, "\s", " ") ;把所有空白符换成空格,因为如果有回车符的话,json转换时会出错
NativeString:=Trim(ss)
MsgBoxStr:=NativeString?lang_yd_translating:""

DetectHiddenWindows, On ;可以检测到隐藏窗口
WinGet, ifGuiExistButHide, Count, ahk_id %transGuiHwnd%
if(ifGuiExistButHide)
{
ControlSetText, , %MsgBoxStr%, ahk_id %transEditHwnd%
ControlFocus, , ahk_id %transEditHwnd%
WinShow, ahk_id %transGuiHwnd%
}
else
{
Gui, new, +HwndtransGuiHwnd , %lang_yd_name%
Gui, +AlwaysOnTop -Border +Caption -Disabled -LastFound -MaximizeBox -OwnDialogs -Resize +SysMenu -Theme -ToolWindow
Gui, Font, s10 w400, Microsoft YaHei UI ;设置字体
Gui, Font, s10 w400, 微软雅黑
gui, Add, Button, x-40 y-40 Default, OK
Gui, Add, Edit, x-2 y0 w504 h405 vTransEdit HwndtransEditHwnd -WantReturn -VScroll , %MsgBoxStr%
Gui, Color, ffffff, fefefe
Gui, +LastFound
WinSet, TransColor, ffffff 210
Gui, Show, Center w500 h402, %lang_yd_name%
ControlFocus, , ahk_id %transEditHwnd%
}

if(NativeString)
{
SetTimer, ydApi_cus, -1
return
}

Return

ydApi_cus:
SetFormat, integer, H

; 获取音标
sendStr := "https://dict.youdao.com/jsonapi_s?doctype=json&jsonversion=4&le=en&q=" . UTF8encode(NativeString) . "~"
whr := ComObjCreate("Msxml2.XMLHTTP")
whr.Open("POST", sendStr, False)

try
{
whr.Send()
}
catch
{
MsgBoxStr:= lang_yd_errorNoNet
goto, setTransText_cus
}

responseStr := whr.ResponseText
transJson := JSON.Load(responseStr)

MsgBoxStr := NativeString

if(transJson.fanyi){
MsgBoxStr := % MsgBoxStr . "`r`n`r`n" . lang_yd_trans . "`r`n`r`n" ;分隔,换行
MsgBoxStr := % MsgBoxStr . transJson.fanyi.tran . "`r`n`r`n"

goto, setTransText_cus
}

if(transJson.ec){
if(transJson.ec.word.usphone){
MsgBoxStr := % MsgBoxStr . " " . "[" . transJson.ec.word.usphone . "] " ;读音
}

if(transJson.ec.word.ukphone){
MsgBoxStr := % MsgBoxStr . " " . "[" . transJson.ec.word.ukphone . "] " ;读音
}

MsgBoxStr := % MsgBoxStr . "`r`n`r`n" . lang_yd_trans . "`r`n`r`n" ;分隔,换行

for index , web_tran in transJson.ec.web_trans
{
if(index > 1) {
MsgBoxStr:=% MsgBoxStr . A_Space . ";" . A_Space
}

MsgBoxStr := % MsgBoxStr . web_tran
}

if(transJson.phrs){
MsgBoxStr := % MsgBoxStr . "`r`n`r`n" . lang_yd_phrase . "`r`n`r`n" ;分隔,换行

for index, entry in transJson.phrs.phrs
{
MsgBoxStr := MsgBoxStr . entry.headword . ": " . entry.translation . "`r`n`r`n"
}
}

goto, setTransText_cus
}

MsgBoxStr := lang_yd_errorNoResults

setTransText_cus:
ControlSetText, , %MsgBoxStr%, ahk_id %transEditHwnd%
ControlFocus, , ahk_id %transEditHwnd%
return
}

0 comments on commit eef0e02

Please sign in to comment.