Post date: Jan 8, 2015 2:02:44 AM
Hackerspace has an article entitled "Highlight and Google Translate Any Text in Linux" by Victor Clark which outlines how you can get instant translations from Google using a bash script with notify-send and xsel.
Install notify-send and xsel using apt-get or the software store
sudo apt-get install notify-send xsel
Copy the script using the plain text editor gedit
Set the script to executable by right clicking it and selecting Properties - Permissions - Allow Executing File as a Program
You can set a system key shortcut to initiate the script. You can also use Read Text Extension to translate the highlighted text following the steps below.
Use the following script and save it as seltr.sh. The script below differs from the original script. it takes the language to translate to from the system language environment instead of just defaulting to English. It uses https instead of http for added security. The script also uses a LibreOffice notification icon.
#!/usr/bin/env bash
# To comply with Google API license:
echo "Powered by Google TM"
l1=$LANGUAGE
l2=${l1:0:2}
notify-send --icon='/usr/share/icons/gnome/scalable/apps/libreoffice-main.svg' -u critical "$(xsel -o)" "$(wget -U "Mozilla/5.0" -qO - "https://translate.google.com/translate_a/t?client=t&text=$(xsel -o | sed "s/[\"'<>]//g")&sl=auto&tl=$l2" | sed 's/\[\[\[\"//' | cut -d \" -f 1)"
To use the script with LibreOffice, install the Read Text Extension, then set up the Tools - Add-ons - Read Selection... menu as shown. In the Read with an external program box, enter the path to the script. In the illustration "Powered by Google™" was entered into the Command line options box -- the bash script ignores the options in this box, so you could enter any random text.
Now, when you click OK, the Desktop shows a notification instead of reading the text aloud. To return the settings to their defaults, type some random characters into the Read with an external program box, and click OK. The program will show an error message and reset the dialogue to the default settings.
Clark, Victor. "Highlight and Google Translate Any Text in Linux" Hackerspace. New York : Kinja, October 21, 2014. Accessed January 7, 2015.