I recently stumbled upon an interesting tiny old script which can translate any text you select. After highlighting the text, be it in a web browser, Libreoffice or PDF document and so on, upon using a keyboard shortcut, its translation is displayed in a desktop notification.
The original script, which dates back to 2012, no longer works however, I've managed to fix it and decided to share it with you.
What makes this very useful is that it allows translating any text you can highlight, be it in a web browser, ODT or PDF document, terminal and so on, and it's easy to access, by simply using your own custom keyboard shortcut.
The script is great for translating words and small phrases, but don't expect it to translate entire web pages because it can't - and there are other tools for that anyway.
The script is great for translating words and small phrases, but don't expect it to translate entire web pages because it can't - and there are other tools for that anyway.
There are a couple of other downsides. If Google cannot determine the source language, it will return an error and thus, it won't translate your text. Also, you need an Internet connection for this to work.
Despite this, the script can be very useful in multiple situations, and once set up, you'll notice how easy it is to use and quickly translate that phrase that you can't quite understand from French, Spanish, Italian or any other language supported by Google Translate.
Despite this, the script can be very useful in multiple situations, and once set up, you'll notice how easy it is to use and quickly translate that phrase that you can't quite understand from French, Spanish, Italian or any other language supported by Google Translate.
Setting up and configuring the 'translate highlighted text' script
1. To be able to use the script, firstly install libnotify-bin (so the script can send desktop notifications), wget (to retrieve the translation from Google) and xsel (which is used to get the currently highlighted text). In Ubuntu, Linux Mint, etc. install them using the following command:
sudo apt-get install libnotify-bin wget xsel
2. Next, copy the script code below:
#!/usr/bin/env bash
notify-send --icon=info "$(xsel -o)" "$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=$(xsel -o | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2, $6}')"
and paste it in a new file - let's call it "notitrans" (well, you can call it whatever you want, but that's how I'll refer to it from now on).In the script above, replace "tl=en" with the language into which you want the text to be translated, for instance "tl=ru" for Russian, "tl=fr" for French and so on.
After you're done, save the file in your home directory and make it executable using the following command:
chmod +x ~/notitrans
3. Place the script in your $PATH - for instance, to copy the script to /usr/local/bin/, use the following command:
sudo mv ~/notitrans /usr/local/bin/
4. To be able to use the script, you can assign it a custom keyboard shortcut. Doing this depends on your desktop environment.
On GNOME (and Unity), you can do this by going to System Settings > Keyboard > Shortcuts > Custom Shortcuts, where you'll need to click "+" to add a new keyboard shortcut. Here, enter any name you want for the new custom shortcut and "notitrans" as the command:
And finally, assign a keyboard shortcut to the newly added command by clicking on it and then holding down the keys you want to assign to it. Make sure the keyboard shortcut is not already in use!
Optional: variations of the 'translate highlighted text' script
It's very easy to modify the script to better suit your needs. Below you'll find a couple of variations I created, which you can further extend. To use the scripts below, simply follow the same steps as above (including changing "en" to the language you want the text to be translated to), but don't copy the script code from step 2 and instead, use the one below.
Display the translation with Zenity (which allows the text to be copied) instead of using desktop notifications:
#!/usr/bin/env bash
text="$(xsel -o)"
translate="$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=$(echo $text | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2, $6}')"
echo -e "Original text:" "$text"'\n' > /tmp/notitrans
echo "Translation:" "$translate" >> /tmp/notitrans
zenity --text-info --title="Translation" --filename=/tmp/notitrans
For this to work, make sure Zenity is installed on your system. On Ubuntu, install it using the following command:sudo apt-get install zenity
Display the translation in a desktop notification AND automatically copy the translation to the clipboard:
#!/usr/bin/env bash
text="$(xsel -o)"
translate="$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=$(echo $text | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2, $6}')"
echo "$translate" | xclip -selection clipboard
notify-send --icon=info "$text" "$translate"
For this to work, make sure xclip is installed on your system. On Ubuntu, install it using the following command:sudo apt-get install xclip
via Lifehacker and Habrahabr