I work with in a company with various other languages to my own (English) and so I use https://translate.google.com a reasonable amount, but as I am on the terminal a lot, I would find a lot of convenience in being able to do that there than having to open a new google tab. The URL structure is trivial, and this works if put into any browser https://translate.google.com/?sl=fr&tl=en&text=bonjour&op=translate, replace fr
by any source language and en
by any target language and bonjoun%20mon%20ami
by any word/phrase. Ideally, I would like 2x functions in bash:
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>
I have tried for a few days without success with lynx
, elinks
etc and many searches on commandlinefu and other sites (e.g. https://www.commandlinefu.com/commands/matching/translate-english/dHJhbnNsYXRlIGVuZ2xpc2g=/sort-by-votes), but not found the trick to returning the translated text. Are Google blocking things somehow, and is there a workaround - surely some tool (lynx
, elinks
, links2
) can resolve the text being sent back when we hit the URL, and then we can extract just the translated text using sed
, cut
, grep
etc?
If this is being blocked by cookies or some sign-on requirements, are there alternative console tools or sites to Google Translate that would allow other translation services?
Various translation services have an API, Google Translate has an API, Deepl has an API. I find some are more accurate than others, but this is a matter of personal preference.
https://www.deepl.com/docs-api
https://cloud.google.com/translate/docs/reference/rest/v2/translate
If you want to use it from shell, it is easy enough to cobble a small bash script with curl
and jq
to process the JSON responses, or better, use Python or Perl which supports all these operations natively.