banner

For a full list of BASHing data blog posts see the index page.  RSS


Revisiting a command-line translator

I don't know who developer Mort Yao is (the "About Me" page on his personal website says nothing), but his command-line translate-shell program has proved to be very useful.

translate-shell is a self-contained executable. It's at version 0.9.6.9 in the Debian stable repos and 0.9.6.12 in Debian testing and unstable, with the later version also available through git.io/trans.

In a Linux Rain post in 2018 I described how I use translate-shell in a script called "trans-paste". The latest version is:

#! /bin/bash
xclip -o | trans -b \
| tee >(yad --geometry=300x800+1525+100 --text-info --wrap) >(xclip -i)
exit 0

I highlight some text and launch "trans-paste" with a keyboard shortcut. A YAD dialog box appears to the right of my main application window, and after a second or two the box fills with a translation from Google Translate (the default translation engine for translate-shell). That translated text is also now on the primary (X) clipboard and I can middle-click-paste it into another document, if I want to. I close the YAD window by pressing Esc.

trans1

The only translate-shell option I have in the script is -b, which returns "the most relevant translation or its phonetic notation only" (quote from the man page). I can also specify the online translation engine with an -e option, but the default is Google Translate and I'm happy with that.

Recently I've been tinkering with translate-shell as an interactive CLI tool to translate what I write from English into other languages. For interactive translation I add to my basic command trans -b the option :[output language code]. (A list of language codes can be found with trans -R.) I enter some text, press Enter and get a Google-translated return. In this mode the translate-shell exit is Ctrl + c. Here I'm going from English to reasonably good German:

trans2

Works fine, but it would nice to differentiate the input text from the translated text. There doesn't seem to be an option for doing this in translate-shell, so I wrapped the program in a simple function, "treg", for turning English into German:

treg() { while true; do read foo; printf "\33[34;1m$(echo "$foo" | trans -b :de)\33[0m"; echo; done; }

trans3

If I'm happy with the translated text I copy it from the terminal and paste it into the German document I'm building. (The ANSI color codes aren't pasted.) The German-to-English function "trge" has only the -b option in the translate-shell command:

trans4

The "treg" and "trge" functions are built with while true; do ... ; done, which keeps repeating whatever follows do until the command is killed with Ctrl + c. The do... in this case is to read user input into the variable "foo", then echo "foo" to the translate-shell command. To make the output from translate-shell bold and blue I printf it with the ANSI color code \33[34;1m followed by a color "reset" with \33[0m.


Last update: 2021-07-28
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License