bash - 在控制台上从一种语言翻译成英语
问题描述
我在一家使用各种其他语言(英语)的公司工作,所以我使用https://translate.google.com的数量很合理,但由于我经常在终端上,我会发现很多方便能够做到这一点,而不是必须打开一个新的谷歌标签。URL 结构很简单,如果将其放入任何浏览器https://translate.google.com/?sl=fr&tl=en&text=bonjour&op=translate,则可以使用fr
任何源语言和en
任何目标语言以及bonjoun%20mon%20ami
任何单词/短语。理想情况下,我想要 bash 中的 2x 函数:
tt (translate to), tt <target-lang> <English word or phrase to translate to target-lang>
tf (translate from), tf <source-lang> <word or phrase to translate to English>
我已经尝试了几天没有成功lynx
,elinks
等等以及在 commandlinefu 和其他网站上的许多搜索(例如https://www.commandlinefu.com/commands/matching/translate-english/dHJhbnNsYXRlIGVuZ2xpc2g=/sort-by-votes),但没有找到返回翻译文本的技巧。谷歌是否以某种方式阻止了某些事情,是否有解决方法——当我们点击 URL 时,某些工具(、、、)肯定可以lynx
解析返回的文本,然后我们可以使用、、等仅提取翻译后的文本?elinks
links2
sed
cut
grep
如果这被 cookie 或某些登录要求阻止,是否有替代谷歌翻译的控制台工具或网站允许其他翻译服务?
解决方案
各种翻译服务都有 API,Google Translate 有 API,Deepl 有 API。我发现有些比其他更准确,但这是个人喜好问题。
https://www.deepl.com/docs-api
https://cloud.google.com/translate/docs/reference/rest/v2/translate
如果你想从 shell 中使用它,很容易拼凑一个小的 bash 脚本curl
并jq
处理 JSON 响应,或者更好的是,使用原生支持所有这些操作的 Python 或 Perl。
推荐阅读
- lua - 有没有办法使用 lua 使用 ESP NOW 协议?
- node.js - 使用 Gun 构建树结构
- android - 相似的屏幕应该是不同的Fragment吗?
- graphql - 未提供所需类型“Type”的变量“$data”
- java - 为什么在没有睡眠的主线程中使用 while(true) 循环时子线程不起作用?
- azure-storage - (已解决)Mule 4 - 转换后发送有效负载到 Azure 文件共享
- assembly - mov [esp], al 中使用的汇编 al
- css - 以前的网络爬虫无法识别元素 ID
- vue.js - 如何更改 Vuetify 的 v-data-table 中特定页面的每页项目的行数?
- c# - 查询工作正常,但 SQLCommand 抛出“无效名称”