Instant offline translation with Argos Translate and a dash of Bash

Google Translate is great, until it's not. The quality of translation is undeniably impressive, and the apps have some genuinely useful features. But even if you choose to ignore the fact that Google lives off our data, there is a matter of Google Translate not working offline—not on a Linux system, anyway. And even if it could do that, there is still a case to be made for using and supporting an open-source alternative free from the shackles of surveillance capitalism.

Enter Argos Translate, an open-source neural machine translation engine that works offline and is available as a Python library, a command-line tool, a web application, and a desktop utility. Install Argos Translate on your machine, add a simple Bash shell script, and you can instantly translate a text selection from any application. Here is how to do this on Ubuntu and Linux Mint.

First step is to install the required packages, which can be done using the following command:

sudo apt install python3-pip xclip

Next, install Argos Translate and the desired language pairs. The following commands install the Argos Translate command-line tool along with a GUI desktop utility, fetch English-German language pairs, and add the ~/.local/bin directory (that's where Argos Translate is installed) to the PATH:

pip install argostranslate
pip install argostranslategui
argospm update
argospm install translate-en_de
argospm install translate-de_en
echo "export PATH=$HOME/.local/bin:$PATH" >>$HOME/.bashrc

To check whether Argos Translate works correctly, open the terminal and run the argos-translate --from en --to de "Hello World!" command. The output should contain the translation.

Run the following commands to create the ~/bin directory and add it to the PATH:

mkdir -p $HOME/bin
echo "export PATH=$HOME/bin:$PATH" >>$HOME/.bashrc

Now, on to the shell script. Use your preferred text editor to create an empty text file and paste the code below into it:

#!/usr/bin/env bash
if [ ! -x "$(command -v argos-translate)" ] || [ ! -x "$(command -v xclip)" ]; then
    echo "Make sure that Argos Translate and xclip are installed on your system"
    exit 1
if [ -z "$1" ]; then
    s=$(xclip -o)
echo -e "$s" >> de-en.txt
echo "---" >> de-en.txt
argos-translate --from de --to en "$s" | xclip
t=$(xclip -o)
echo -e "$t\n" >> $HOME/de-en.txt
notify-send "$t"

Save the file under the de-en name in the ~/bin directory. Make the script executable using the chmod +x ~/bin/de-en command.

We're almost done. The last step is to assign a keyboard shortcut to the script. To do this on Linux Mint Cinnamon, open the System Settings control center, switch to the Keyboard > Shortcuts section, and create a new shortcut under Custom Shortcuts. Make sure that the shortcut points to the ~/bin/de-en file.

That's all there is to it. Select a text you want to translate, press the specified shortcut, and you should see the translation in a popup notification. The script saves all text selections and their translations in the de-en.txt file, in case you need to review them later.