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
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.