I need to work with some text files (source code) that have comments in a language that is foreign to me, and I am wondering if there is a text editor available that has some built-in support to make translation easier.

I can copy and paste to google translate running in a browser in another window and then copy and paste back again, but it would be much easier if I could highlight, right click and choose "translate...", or something like that!

I'm not looking for automatic translation of the whole file, just the bits I highlight.

Kubuntu 18.01, and Kate is my usual editor of choice but I'd be happy to use another editor for this job if it had some helpful features.

  • Did you come across hackerspace.kinja.com/…? It still uses Google Translate but in a more convenient way. – DK Bose Sep 22 '18 at 12:48
  • @DK Bose thank you, I can do something based on that and as a side benefit it was quite instructive (using wget to drive google translate, and using xsel to access highlighted text/the clipboard from a script). Would be happy to accept this as the answer if you would care to present it as such. – B.Tanner Sep 22 '18 at 14:17
  • I'll take a little time to do so, since it's new to me as well! – DK Bose Sep 22 '18 at 14:25
  • Sorry! I tried it but could only get the part I highlighted to show up in the notification and not the translation. Also, the notification does vanish after a couple of seconds. Of course, one can examine the notification history but, as I said, I couldn't get the translation part to work! Whether it's because I have a slow net speed, I can't say. If you get it to work, please feel free to post the answer yourself. – DK Bose Sep 22 '18 at 14:54
  • 1
    Yes I'm getting a "forbidden" HTTP return - I see google are now offering a paid-for translation service which no doubt has something to do with it. According to ctrlq.org/code/19909-google-translate-api there is an unofficial equivalent that the google chrome translate plugin uses but it all seems tied in with Javascript. I have had a translation back but only in an obscurely-named file. I will play a little more but it's getting to the point where it would be quicker to what I was trying to do the long cut-and-paste way! – B.Tanner Sep 22 '18 at 16:37

Here's one way using translate-shell which is in the Bionic multiverse repository. The homepage is here.

You may need to have gawk, curl, and xsel on your system.

There are a lot of details on the homepage and probably a more efficient way to do things, but this is what I got:

#!/usr/bin/env bash

# source: https://github.com/soimort/translate-shell
# sudo apt install translate-shell
# also needs curl, xsel, and gawk
# sample: https://pl.lipsum.com/

trans -brief "$(xsel -o)" > temp.txt

echo -n "$(cat temp.txt)" | xsel -b -i

In Kubuntu 18.04, I called this script trans.sh, saved it to ~/bin, made it executable, and bound it to Meta+U using the Shortcuts > Custom Shortcuts in System Settings.

On a page such as https://pl.lipsum.com/, I highlight some text, move to the location in the destination text editor (including Kate), press Meta+U, wait a couple of seconds, and then press Ctrl+V. The translated text is pasted in at the cursor position.


sample text to be translated

What is Lorem Ipsum?

Lorem Ipsum is a text used as an example filler in the printing industry. It was first used in the 15th century by an unknown printer to fill in a text of a test book. Five centuries later, the electronics industry began to be used, remaining virtually unchanged. Popularized in the 1960s with the publication of Letrasetu sheets, containing fragments of Lorem Ipsum, and recently with the different versions of Lorem Ipsum containing software designed to implement prints on personal computers, such as Aldus PageMaker

Note: in the example script, I've used " but you may prefer using ' unless ' occurs internally as mentioned in the home page:

To avoid punctuation marks (e.g. "!") or other special characters being interpreted by the shell, use single quotes:
There are some cases though, you may still want to use double quotes: (e.g. the sentence contains a single quotation mark "'")

  • 1
    thank you so much, that is exactly what I was after, it is working very well for me and may well turn out to be useful beyond my immediate requirement. – B.Tanner Sep 23 '18 at 11:59
  • You're most welcome. Please let me know if you make it more efficient! – DK Bose Sep 23 '18 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.