Google Translate CLI: Translating at the command line

Multilingual

© Lead Image © Cienpies Design, 123RF.com

© Lead Image © Cienpies Design, 123RF.com

Article from Issue 165/2014
Author(s):

If you regularly work in a terminal and need to translate sections of text, you can run the Google Translate CLI tool to avoid annoying changes to the browser window.

When working with Linux, you may often come across text in foreign languages, such as instructional pages or output from various tools, created by someone who works in a language other than your native language. In these cases, you usually resort to looking up individual terms and phrases on translation sites  – together with time-consuming paging back and forth between the console and web browser. In such cases as these, Google Translate CLI comes to the rescue: With just one call, it conveniently translates single words or whole blocks of text directly at the command line.

Installation

The best-known tool for straightforward translation of terms and texts is Google Translate: You type the source text in the left-hand box, and the box on the right shows you the real-time translation in the target language [1]. Although the browser version of the translation project is available for free, Google also offers a commercial API that you can embed in your own applications. Besides the financial aspect, however, the API has another disadvantage: Each user requires a separate API key, which makes this version almost totally uninteresting for free translation tools.

Google Translate CLI [2] therefore deliberately takes the roundabout route of accessing the Google Translate website directly by means of a script. Thus, you do not need to have your own API key, yet you still receive a response in a brief and precise form  – much as you would through an API.

Under the hood, Google Translate CLI relies on a very lean GNU Awk script with just 108 lines. Installation is a matter of a few simple steps (Listing  1).

Listing 1

Installing Google Translate CLI

 

It Works!

After installing Google Translate CLI, you can call it at the command line using the translate command, or trs for short, followed by the words you want to translate. If you enter multiple terms, the tool translates them one after another, without making any associations. However, if you type a phrase in quotes, Google Translate CLI interprets it as a sentence and treats it accordingly (Listing  2).

Listing 2

Sample Translation

 

By default, the script translates into English; it automatically detects the source language. If you want to translate into any other language, you need to specify the appropriate language code in curly brackets, for example:

$ trs {=de} bonjour

The Google Translate website provides a detailed list of languages and their associated codes [3]. Optionally, you can specify multiple target languages in a single call by concatenating the language codes with plus signs (Figure 1). If you use Bash as the default shell, it is important to escape the exclamation point with a backslash (\!).

Figure 1: Using the Google translator opens up interesting additional options, such as simultaneous output in different languages, which not even the browser version offers.

If you want to translate words or text into more exotic languages, you must first install the appropriate fonts. Calling trs without parameters outputs a brief help text.

Versatile

Thanks to its ease of use, Google Translate CLI can be integrated effortlessly into your everyday workflow with Linux. Fundamentally, the tool supports even more uses than simply looking up individual terms at the command line. For example, you can embed it in your own scripts to automatically translate your own wiki, for example.

Google Translate CLI allows for this application diversity because it also supports file-based input. For example, the following call translates into French:

$ trs {=fr} <filename>

The outcome of the action is shown in Figure 2. In the test, I noticed that you need to quote proper names to prevent Google Translate CLI from translating them to the extent possible. If you translate man pages or the output from other command-line tools directly with the tool without redirecting to a file, you will rarely receive meaningful or complete feedback.

Figure 2: Google Translate CLI also provides simple support for translating files.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News