banner

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


Polyglot and round-trip translations

translate-shell is a command-line program for text translation using online services such as Google Translate. I've previously shown how translate-shell can be used for quick translations of highlighted text in a separate window, and for interactive translations of typed-in text in a terminal.

To allow for polyglot (multiple) translations from English into any number of other languages, I wrote a new function that specifies the other languages with ISO codes. (You can find these codes through translate-shell by entering trans -R; "de" is German, "el" is Greek, etc.)

The basic version of the new function is:

poly() { echo "$1"; for i in "${@:2}"; do trans -b :"$i" <<<"$1"; done; }

Below "poly" is translating into German, Spanish, Greek, Korean, simplified Chinese and French using Google Translate, the default translation service:

trans1

The English original is printed (echo "$1"); it's the first argument for "poly". Next there's a for loop that iterates through all the arguments beginning with the second one (for i in "${@:2}"). Each of those arguments (like "de", "es", "el") is fed to trans in the command trans -b [option for translation only] :[language argument] <<<"[text to be translated]".

Another version of the function separates the results more neatly:

polyN() { printf "..........\n$1\n..........\n"; for i in "${@:2}"; do trans -b :"$i" <<<"$1"; printf "..........\n"; done; }

trans2

Just for fun, I wrote a function which does round-trip translating of English through any number of languages in sequence, then back to English. It's a polyglot variation on the "telephone" game, or "Chinese whispers":

polyRT() { echo "$1"; trans -b :"$2" <<<"$1" | tee >(xclip -i); for i in "${@:3}"; do xclip -o | trans -b :"$i" | tee >(xclip -i); done; xclip -o | trans -b; }

Here again the first argument (the text to be translated) is printed.
 
The next command (trans -b :"$2" <<<"$1" | tee >(xclip -i)) translates the original text into the language of the second argument, then passes the result to tee, which prints the translation to stdout and also passes it to the xclip utility.
 
Reddit user "geirha" points out that tee >(xclip -i) can be replaced with xclip -i -f. The -f option (from the xclip man page): "when xclip is invoked in the in mode with output level set to silent (the defaults), the filter option will cause xclip to print the text piped to standard in back to standard out unmodified".
 
The third command (for i in "${@:3}"; do xclip -o | trans -b :"$i" | tee >(xclip -i); done) is a for loop that iterates through the language arguments beginning with the third one. For each argument, trans translates the contents of the primary clipboard (stored in xclip) into the language of the current argument, and again this result is passed to tee and on to xclip.
 
The final command (xclip -o | trans -b) translates the text from the last argument's language into English.

In the demonstration below, a test statement is translated from English to German, from German to Spanish, from Spanish to Korean, from Korean to Chinese, from Chinese to Japanese, and from Japanese to English. Not a perfect round-trip, but close:

trans3

From the Wikipedia article on "Chinese whispers":

In 2012 a global game of Chinese Whispers was played spanning 237 individuals speaking seven different languages. Beginning in St Kilda Library in Melbourne, Australia, the starting phrase "Life must be lived as play" (a paraphrase of Plato) had become "He bites snails" by the time the game reached its end in Alaska 26 hours later.

OK, here's "Life must be lived as play" going through Dutch, northern Kurdish, Mongolian, Latvian, Filipino, Cantonese and Danish before returning to English:

trans4

I think something got lost in translation there...


Last update: 2024-05-31
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License