Fun ways to incorporate FOSS front ends to translating services inside your scripts.

dmenu shell tutorial

DMENU MEISTERSHTIK I. - translation

Fun ways to incorporate FOSS front ends to translating services inside your scripts.

dmenu

dmenu is by default a little bar at the top of your screen that launches installed programs. But it also lets you customize its input and output to virtually no limits. It's an interactive prompt - though all the interactivity depends on you and your skill.

dmenu lives here: tools.suckless.org/dmenu, it also probably exists as a package in your favorite OS, but you're encouraged to build it yourself, since the best way to customize it is by editing the source before you build it. But I won't be covering it here. If you're reading this, I am at least 83% convinced you already know what dmenu is.

I won't keep you long. I was planning for this upcoming DMENU series to be a lengthy guide on writing your own links2 companion, but to get you acquainted with these 'baby's first hacking steps', I decided to keep it simpler than simple. In this example, we will use dmenu to translate a word or a phrase and have it return the result, utilising one of the publicly available simplytranslate instance.

One of the big PROs as to why these things are so easy to make, is thanks to a variety of FOSS front-ends to otherwise terribly incompatible services. We don't need any APIs, javascript magic, or dark-techno-wizardry. We just need a URL that we can bend to our will.

simplytranslate

You'll need dmenu, curl and sed. And shell...

simplytranslate is a survivor of the simple-web.org armageddon, a site that hosted many 'simple' front-ends to several services. Sadly it went dark sometime in 2023, but the projects were kept alive.

simplytranslate is a front end to, among others, google translate. It's designed with browsers in mind, but we won't need one for our dmenu script.

Find your favorite instance here: farside.link, I'll be using translate.bus-hit.me, because I think the url is funny.

So, what I want to happen is the following:

And this is how we get it done:

#!/bin/sh
instance="translate.bus-hit.me"
from="en"
to="cs"

input=$(dmenu < /dev/null)
input=$(echo "$input" | sed 's/ /%20/g')

curl -s "https://$instance/?engine=google&from=$from&to=$to&text=$input" \
  | grep -E "</textarea>" | cut -d"<" -f1 | dmenu -l 2

what happened?

So, what exactly happened there?

There are three variables at the top, 'instance', 'from' and 'to'. In this example I'm using the aforementioned bus-hit.me instance and am translating from English to Czech. You probably want to change those variable to languages relevant to you.

Next up we define the 'input' variable, which will be whatever we type into the dmenu prompt that shows up.

The 'input sed' line swaps all 's p a c e s' with '%20'. curl doesn't like spaces.

And the very last line returns the html source for a webpage which has our result, greps only the relevant lines and clips off the html tag at the end.

Stay tuned, it's only going to get more complicated.