- 安装 xsel,
sudo apt install xsel
- 安装 translate-shell
然后把以下代码复制进一个notify-send.sh文件中,
#!/usr/bin/env bash
# Need https://github.com/soimort/translate-shell installed.
se=$(xsel -o)
re=$(/usr/bin/trans -b :zh "$se")
notify-send "$se" "$re" -i accessories-dictionary -t 2000
chmod a+x notify-send.sh
然后在系统设置中添加快捷键运行 notify-send.sh 脚本。
打开任意软件,选词,按下快捷键,就会弹出系统通知显示翻译了。
脚本中se
存储选中的词,re
存储翻译结果。
使用 translate-shell 是获取翻译的一种方式,使用其他方式也可以。比如用 wget 调用谷歌翻译或百度翻译的 API,或者写 Python 脚本打开谷歌翻译网址,解析页面获取翻译等等。
使用系统通知显示翻译结果也只是显示翻译结果的一种方式,也可以写一个 Python 脚本使用 Tkinter 显示结果,鼠标离开时就销毁窗口?
END
2018.5.3