Translate Any Text You Select On Your Linux Desktop With A Keyboard Shortcut And Notifications

Google Translate notification Linux

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.
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.

Setting up and configuring the ‘translate highlighted text’ script

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.

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.

Google Translate keyboard shortcut

 

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 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.

Google Translate keyboard shortcut
#!/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
#!/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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.