aardio快速变量名翻译工具,再也不用频繁的复制粘贴了。
By
小强2233
at 2022-04-04 • 1人收藏 • 1775人看过
很多英文能力比较差的初学者,每次给变量取名字都是需要打开翻译网站一个个的翻译单词,语多的话,需要多次ctrl+c/ctrl+v,非常的麻烦,下面分享一款个人写的小工具。
能够一键把中文翻译成英文,并且生成驼峰格式,支持的不仅仅是aardio ide,理论上支持所有软件界面上的文本编辑框。
大家可以自己扩展添加更多的功能。
import win.ui; import win.ui.atom; import win.ui.menu; import win; import key; import winex; import win.util.tray; import win.clip.viewer; import win.ui.tooltip; import preg; import inet.http; /*DSG{{*/ mainForm = win.form(text = "aardio工程"; right = 959; bottom = 591) mainForm.add() /*}}*/ var atom = mainForm.atom("D73AF3BB-1A17-4C68-8C9B-7B3428344854"); if (!atom) { /*为窗口设置原子值可以避免一个程序重复运行多个实例*/ win.quitMessage(); return; } hkid = mainForm.reghotkey(function (id, mod, vk) { win.delay(200); key.combine("CTRL", "C"); //win.delay(100); var hwnd = win.getForeground(); var hwnd2 = winex.getFocus(); var pt = ::POINT(); winex.attach(hwnd2, true); var ok, pt = ::User32.GetCaretPos(pt); var x, y = win.toScreen(hwnd2, pt.x, pt.y); winex.attach(hwnd2, false); var balloonTipCtrl = win.ui.tooltip.tracking(hwnd2) if (x && y) { balloonTipCtrl.setText("翻译中,请稍候...").trackPopup(true, x - 10, y + 20); } else { balloonTipCtrl.setText("翻译中,请稍候...").trackPopup(true); } var net = inet.http(); var resultStr = net.get("https://translate.google.cn/m?sl=zh-CN&tl=en&hl=zh-CN&q="++inet.url.encode(string.trim(win.clip.read()))); var regex = preg(`/<div class="result-container">(.*?)</div>/1`); var wordlist = string.split(regex.match(resultStr), " "); var tRes = ""; for (k, v in wordlist) { if (k == 1) { tRes = string.lower(v) continue; } tRes += string.replace(v, v[[1]], string.upper(v[[1]]), 1); } if (#tRes >= 1) { win.clip.write(tRes); win.delay(200); key.combine("CTRL", "V"); } else { balloonTipCtrl.setText("翻译失败,请检查网络或者接口!"); win.delay(3000); } balloonTipCtrl.getCurrentTool().trackPopup(false); }, 0x2 /*_MOD_CONTROL*/ | 0x1 /*_MOD_ALT*/ , 'Y'#); mainForm.onMinimize = function (lParam) { mainForm.tray = win.util.tray(mainForm) mainForm.tray.tip = "鼠标提示" //设置鼠标提示 mainForm.show(false); //hideWindow return true; //阻击默认消息传递,取消最小化过程 } var isClose = false; mainForm.onClose = function (lParam) { if (!isClose) { mainForm.tray = win.util.tray(mainForm) mainForm.tray.tip = "鼠标提示" //设置鼠标提示 mainForm.show(false); //隐藏窗口 return true; //阻击默认消息传递,取消关闭过程 } } mainForm.wndproc = { [0xACCF /*_WM_TRAYMESSAGE*/ ] = function (hwnd, message, wParam, lParam) { if (lParam = 0x205 /*_WM_RBUTTONUP*/ ) { var pt = ::POINT();::User32.GetCursorPos(pt); //弹出托盘菜单以前,一定要前置主窗口中,不然不点击菜单不会消失 win.setForeground(mainForm.hwnd) mainForm.popmenu.popup(pt.x, pt.y, true) } } } mainForm.popmenu = win.ui.popmenu(mainForm); //创建弹出菜单 mainForm.popmenu.add('&open', function (id) { mainForm.show(true); }); mainForm.popmenu.add(); //分隔线 mainForm.popmenu.add('&exit', function (id) { isClose = true; mainForm.close(); }) mainForm.show(true); return win.loopMessage();
10 个回复 | 最后更新于 2022-04-11
回复#2 @winnet :
扩展库里有, 自己搜索安装下.
不用扩展库preg的话,直接string模式匹配也可以, 用下面的代码替换下
var resultStr = net.get("https://translate.google.cn/m?sl=zh-CN&tl=en&hl=zh-CN&q="++inet.url.encode(string.trim(win.clip.read()))); var strMatchResult = string.match( resultStr, `\<div class="result-container">(.*?)\<\/div\>` ); var wordlist = string.split(strMatchResult, " ");
改进了一下, 增加了个配置文件, 可以将以前翻译过的保存到ini文件中, 这样就算后面没网了也可以用, 有过记录的也不用每次去联网 ,当你代码翻译的多了, 基本上就不用联网了....
另外在ctrl+c之前清空下剪贴板, 这样当没框选文字和没选中文字的时候就提示没选中文字.
import win.ui; import win.ui.atom; import win.ui.menu; import win; import key; import winex; import win.util.tray; import win.clip.viewer; import win.ui.tooltip; import preg; import inet.http; import fsys.ini; /*DSG{{*/ mainForm = win.form(text = "aardio工程"; right = 959; bottom = 591) mainForm.add() /*}}*/ var atom = mainForm.atom("D73AF3BB-1A17-4C68-8C9B-7B3428344854"); if (!atom) { /*为窗口设置原子值可以避免一个程序重复运行多个实例*/ win.quitMessage(); return; } var WordINI = fsys.ini(io.appData("\WordTransLateLIst\wordList.ini")); hkid = mainForm.reghotkey(function (id, mod, vk) { win.clip.write("");//重写并清空 win.delay(200); key.combine("CTRL", "C"); //win.delay(100); var hwnd = win.getForeground(); var hwnd2 = winex.getFocus(); var pt = ::POINT(); winex.attach(hwnd2, true); var ok, pt = ::User32.GetCaretPos(pt); var x, y = win.toScreen(hwnd2, pt.x, pt.y); winex.attach(hwnd2, false); var balloonTipCtrl = win.ui.tooltip.tracking(hwnd2) var PreTransWord = win.clip.read(); if(PreTransWord!=null and PreTransWord!=''){//剪贴板复制到了文字 var tRes = WordINI.read("wordList",PreTransWord); if( tRes == null ){ if (x && y) { balloonTipCtrl.setText("翻译中,请稍候...").trackPopup(true, x - 10, y + 20); } else { balloonTipCtrl.setText("翻译中,请稍候...").trackPopup(true); } try{ var net = inet.http(); var resultStr = net.get("https://translate.google.cn/m?sl=zh-CN&tl=en&hl=zh-CN&q="++inet.url.encode(string.trim(PreTransWord))); var regex = preg(`/<div class="result-container">(.*?)</div>/1`); var wordlist = string.split(regex.match(resultStr), " "); tRes = ""; for (k, v in wordlist) { if (k == 1) { tRes = string.lower(v) continue; } tRes += string.replace(v, v[[1]], string.upper(v[[1]]), 1); } } } if (#tRes >= 1) { WordINI.write("wordList",PreTransWord,tRes); win.clip.write(tRes); win.delay(200); key.combine("CTRL", "V"); } else { balloonTipCtrl.setText("翻译失败,请检查网络或者接口!"); win.delay(2000); } }else { balloonTipCtrl.setText("未选中任何文字!").trackPopup(true); win.delay(2000); } balloonTipCtrl.close(); }, 0x2 /*_MOD_CONTROL*/ | 0x1 /*_MOD_ALT*/ , 'Y'#); mainForm.onMinimize = function (lParam) { mainForm.tray = win.util.tray(mainForm) mainForm.tray.tip = "鼠标提示" //设置鼠标提示 mainForm.show(false); //hideWindow return true; //阻击默认消息传递,取消最小化过程 } var isClose = false; mainForm.onClose = function (lParam) { if (!isClose) { mainForm.tray = win.util.tray(mainForm) mainForm.tray.tip = "鼠标提示" //设置鼠标提示 mainForm.show(false); //隐藏窗口 return true; //阻击默认消息传递,取消关闭过程 } } mainForm.wndproc = { [0xACCF /*_WM_TRAYMESSAGE*/ ] = function (hwnd, message, wParam, lParam) { if (lParam = 0x205 /*_WM_RBUTTONUP*/ ) { var pt = ::POINT();::User32.GetCursorPos(pt); //弹出托盘菜单以前,一定要前置主窗口中,不然不点击菜单不会消失 win.setForeground(mainForm.hwnd) mainForm.popmenu.popup(pt.x, pt.y, true) } } } mainForm.popmenu = win.ui.popmenu(mainForm); //创建弹出菜单 mainForm.popmenu.add('&open', function (id) { mainForm.show(true); }); mainForm.popmenu.add(); //分隔线 mainForm.popmenu.add('&exit', function (id) { isClose = true; mainForm.close(); }) mainForm.show(true); return win.loopMessage();
登录后方可回帖
挺不错,赞一个,(●—●)