Unix Translate Notifications: An elegant method to get translations

Posted on Apr 1, 2020

Summary (TL;DR)

You can translate any selected text and easily display it as a Linux notification using a simple one-liner script with Translate Shell. Feel free to adjust the parameters as needed.

I’ve assigned this script to a key in my own setup.

$ xsel | \
trans :tr -b -no-ansi | \
xargs -0 notify-send -u normal --app-name "Translate :tr" --icon=_ -t 5000 "🏳 Translate"

Abstract

As a non-native English speaker, I often encounter unfamiliar words and phrases in certain contexts while reading articles, books, etc., which can be distracting.

So I needed a simple translate.

Solution attempts did not work

  • Using translate apps (such as Google Translate):
    • These apps occupy screen space and requires frequent window/content switches.
    • They are overly complex for my relatively simple problem.
  • Translate popup plugins/addons in web browsers:
    • They request excessive permissions for website data.
    • Limited to web browsers, occasionally unstable.

Solution: Translate Notification

The solution is creating a simple script that translates and prints into notification and assign this script into key.

Dependencies

  • xsel for getting selected text.
  • Translate Shell to translate. Github Link.

Implementation

  1. Get selected text.
  2. Translate it (unknown to Turkish in my case).
  3. Print it on Linux notification.

If you combine these operations with pipe:

$ xsel | \
trans :tr -b -no-ansi | \
xargs -0 notify-send -u normal --app-name "Translate :tr" --icon=_ -t 5000 "🏳 Translate"

Here you are. I’m using i3wm so I assigned it to $mod+Shift+f key on my i3 config file:

bindsym $mod+Shift+f exec xsel | trans :tr -b -no-ansi  | xargs -0 notify-send -u normal --app-name "Translate :tr" --icon=_ -t 5000 "🏳 Translate"
comments powered by Disqus