首页 > 解决方案 > 在控制台上从一种语言翻译成英语

问题描述

我在一家使用各种其他语言(英语)的公司工作,所以我使用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>

我已经尝试了几天没有成功lynxelinks等等以及在 commandlinefu 和其他网站上的许多搜索(例如https://www.commandlinefu.com/commands/matching/translate-english/dHJhbnNsYXRlIGVuZ2xpc2g=/sort-by-votes),但没有找到返回翻译文本的技巧。谷歌是否以某种方式阻止了某些事情,是否有解决方法——当我们点击 URL 时,某些工具(、、、)肯定可以lynx解析返回的文本,然后我们可以使用、、等仅提取翻译后的文本?elinkslinks2sedcutgrep

如果这被 cookie 或某些登录要求阻止,是否有替代谷歌翻译的控制台工具或网站允许其他翻译服务?

标签: bashgoogle-translatelynxelinks

解决方案


各种翻译服务都有 API,Google Translate 有 API,Deepl 有 API。我发现有些比其他更准确,但这是个人喜好问题。

https://www.deepl.com/docs-api

https://cloud.google.com/translate/docs/reference/rest/v2/translate

如果你想从 shell 中使用它,很容易拼凑一个小的 bash 脚本curljq处理 JSON 响应,或者更好的是,使用原生支持所有这些操作的 Python 或 Perl。


推荐阅读