How To Translate Highlighted Text Using A Script

If you are familiar with Google Translate, you will realize how convenient it is to just copy a phrase or sentence and have it translated but what if you can do the same task without having to move from source to the Google translate section on Google search by just highlighting the section of a PDF file, phrase on a web page or ODT and even in the terminal by having the translation displayed.
There is a method and if followed correctly will allow you translate any highlighted text while using any Linux-based operating system.

Though it comes with a few downsides like requiring an internet connection and receiving error messages when Google can’t determine the source language.

Follow the following steps

Step One

First, you need to install libnotify-bin, this will enables the script to send desktop notifications. Then download wget, this retrieves the translation from Google and xsel, which is used to grab the currently highlighted text.
After getting all these, install them by using this command in the terminal:
sudo apt-get install libnotify-bin wget xsel

Step Two

What you need to do now is copy this script code:
#!/usr/bin/env bash
notify-send –icon=info “$(xsel -o)” “$(wget -U “Mozilla/5.0” -qO – “$(xsel -o | sed “s/[\”‘<>]//g”)” | sed “s/,,,0]],,.*//g” | awk -F'”‘ ‘{print $2, $6}’)”

When the code is copied, paste it into a new file — you can give it any name you want.
In the code above, replace the “tl=en” to the language you want the text highlighted to be translated to. Example “tl=ru” if you want it translated to Russian, follow the same procedure for every language you want your text translated to.
When you’re done, you can now save the file in the home directory and convert it to an executable using this command:
chmod +x ~/notitrans
Notitrans is the name given to file but remember, you can use any name you wish.

Step Three

What you need to do now is copy the script to the /usr/local/bin/ which is the $PATH by using this command:
sudo mv ~/notitrans /usr/local/bin/

Step Four

Now what you need to do is assign a custom keyboard shortcut to enable the script and depending on the desktop environment you are using, the method might vary.
For those using GNOME and Unity, you can by following these steps. Head over to System Settings>Keyboard>Shortcuts>Custom Shortcuts then click on the + icon to add a new keyboard shortcut, then name the shortcut. Now you can assign an open shortcut by holding down the key combination you want to use.


About The Author
Lamin Kanteh
Lamin loves smartphones and was introduced to the world of mobile devices with Android. Ubuntu is new to him, but he is enjoying writing about it so far.

Leave a Response