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.
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.
Source:
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 "'")
No comments:
Post a Comment