Do you translate blocks of text by copying them and pasting into Google Translate in a browser? If you do, the script explained here might save you a lot of work.
At the core
The script relies on translate-shell, a wonderful command-line utility written by Mort Yao and available in the repositories of most distros. On the command line, translate-shell looks up text in an online translator and returns a translation in the home language of your locale, plus (by default) other information. The command is trans and the argument is the text to be translated:
There are quite a few options for translate-shell, including one that speaks both the original text and the translation using text-to-speech, although this only works if you have a command-line application that can play streaming audio, like the excellent mpv.
It's just the basic translation feature of translate-shell that's used in the following script.
I call the script trans-paste. I highlight a block of text to be translated and enter a keyboard shortcut (I use Ctrl+Alt+P) to launch the script. A dialog box pops up to the right of my working window with the translation. To close the box ready for a second translation I press Esc.
#!/bin/bash xclip -o | trans -e google -b \ | tee >(yad --geometry=300x800+1550+100 --text-info --wrap) >(xclip -i) exit 0
The script grabs the selected text with the xclip utility and pipes it to translate-shell. The translation utility gets a translation online from Google Translate (with the "-e google" option) without opening a browser, and returns only the translation (with the "-b" option). The translated text is sent to a YAD dialog window positioned just where I want it on my desktop. (My working windows are all maximised to a right-hand margin of 1520 px on my 1920 px display, leaving 400 px on the right of a working window for pop-ups like the translation box.)
But wait — there's more. The script also loads xclip with the translated text, so that the text can be middle-click-pasted into either the original document or any other application that accepts plain text. The pop-up window can be closed, but the copied translation text will remain in the X clipboard until that clipboard is filled again with a new text selection.
Note that the tee command is used in the script to send the output of translate-shell to two different commands using process substitution. For a very clear explanation of the use of tee as an alternative to named pipes, see this article by K. Ghose.