Google Translate CLI: Translating at the command line
Multilingual
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 (\!
).
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.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Endless OS 6 has Arrived
After more than a year since the last update, the latest release of Endless OS is now available for general usage.
-
Fedora Asahi 40 Remix Available for Macs with Apple Silicon
If you've been anticipating KDE's Plasma 6 for your Apple Silicon-powered Mac, then you're in luck.
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.