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
-
First Release Candidate for Linux Kernel 6.14 Now Available
Linus Torvalds has officially released the first release candidate for kernel 6.14 and it includes over 500,000 lines of modified code, making for a small release.
-
System76 Refreshes Meerkat Mini PC
If you're looking for a small form factor PC powered by Linux, System76 has exactly what you need in the Meerkat mini PC.
-
Gnome 48 Alpha Ready for Testing
The latest Gnome desktop alpha is now available with plenty of new features and improvements.
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.