Text Language Translation Utility

Ever since moving to Europe back in 2010, I have been a dedicated user of Google Translate. However, cutting-and-pasting between a web site or text document and the Translate’s input field is a bit tedious, so I looked into ways to make it easier to integrate into my daily life on a laptop. The solution I came up with relies on Python, the Google translate API, and the excellent macOS utility called Keyboard Maestro.

Getting Started with Translations

There is some initial setup necessary before Google translate API can be used. I followed the instructions for the Python client library. Next, I created a Python virtual environment in which to install the Google Python package. A short bit later, I was able to run a simple Python program that does the job:

import sys
from google.cloud import translate_v2 as translate

def translate_text(text: str, source: str, target: str) -> str:
    result = translate.Client().translate(values=text, target_language=target, source_language=source)
    return result['translatedText']

if __name__ == '__main__':
    print(translate_text(sys.stdin.read().strip(), sys.argv[1], sys.argv[2]))
    sys.exit(0)

Nothing fancy – just read in some text to translate and direct Google Translate to do the hard work. When we get the response from Google, send the output text to stdout for another process to read. We can quickly check that it works:

echo 'The king is dead. Long live the king' | python translate.py en fr
Le roi est mort. Longue vie au roi.

Seems ok. Let’s try the reverse for fun:

echo 'Le roi est mort. Longue vie au roi.' | python translate.py fr en
The king is dead. Long live the king.

To make things easy when calling this from another process, I created a trampoline script to setup the Python environment before invoking the above Python code:

#!/bin/bash

ROOT=$(dirname "${BASH_SOURCE[0]}")
source "${ROOT}/venv/bin/activate"
exec python "${ROOT}/translate.py" "${1}" "${2}" 2> /dev/null

Bring in the Keyboard Maestro

Next step is to hook the trampoline script (translate) to run when I press a specific key sequence after selecting some text. First, I created two Keyboard Maestro macros for the two translation paths: English to French, and French to English.

The only difference is the order of the en and fr arguments given to translate shell script.

The macros read whatever text is in the System Clipboard and passes it to the translate shell script. Keyboard Maestro reads the output of the Python process and puts it into the System Clipboard, replacing the text that was there with its translation.

Both macros are configured to appear in the global macro palette. The global macro palette will appear when a key sequence is pressed, which in my setup is the function key F12.

Now, Pressing F12 brings up a small palette with two choices:

Pressing the letter E will bring about the English-French translation, while pressing F will translate French to English. Simple. Here is what the translation output looks like: