Highlight and Google Translate Any Text in Linux

By Lifehacker on at

The Google Translate extension for Chrome has been updated with a fine "highlight-and-translate" feature. This may be quite handy when you need the translation of a separate word without opening any addition tabs. Google Translate may be one of the best translation tools and the updated extension is great, but it only translates text when Chrome detects that your current page is in another language than the ones set in the browser settings. Not to mention that this will only work inside Chrome, completely ignoring everything that's going on inside the system.

In Windows you can install a separate program that will translate the highlighted text, but you still need to buy, install it with dictionaries and run it. However, in Linux you can get the translation of any highlighted text in form of a notification message. All you need is a little script. Here's how to make it.

Highlight and Google Translate Any Text in Linux

1. Install the necessary package for notifications (you probably already have this one if you're using Ubuntu):

sudo apt-get install libnotify-bin xsel

2. Create a document entitled seltr with these 2 lines:

#!/usr/bin/env bash

notify-send -u critical "$(xsel -o)" "$(wget -U "Mozilla/5.0" -qO - "http://translate.google.com/translate_a/t?client=t&text=$(xsel -o | sed "s/[\"'<>]//g")&sl=auto&tl=eng" | sed 's/\[\[\[\"//' | cut -d \" -f 1)"

The -u critical parameter is required for the notification to be displayed above other windows;

sl=auto auto detect source language;

tl=eng translation language (English).

3. Make this file executable and move it to /usr/bin/:

sudo chmod 777 ./seltr && sudo mv ./seltr /usr/bin/

4. Assign hotkeys for seltr using built-in tools or via xbindkeys.

Now all you need to do is to select any text inside any window and hit your hotkey.

Highlight and Google Translate Any Text in Linux

The coolest thing about this translation method, is that you don't need any additional software as it uses Google Translate directly (hence you need the Internet connection). It works inside any window and the translation is within one hotkey reach.