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
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.
-
Linux Foundation Adopts OMI to Foster Ethical LLMs
The Open Model Initiative hopes to create community LLMs that rival proprietary models but avoid restrictive licensing that limits usage.
-
Ubuntu 24.10 to Include the Latest Linux Kernel
Ubuntu users have grown accustomed to their favorite distribution shipping with a kernel that's not quite as up-to-date as other distros but that changes with 24.10.
-
Plasma Desktop 6.1.4 Release Includes Improvements and Bug Fixes
The latest release from the KDE team improves the KWin window and composite managers and plenty of fixes.
-
Manjaro Team Tests Immutable Version of its Arch-Based Distribution
If you're a fan of immutable operating systems, you'll be thrilled to know that the Manjaro team is working on an immutable spin that is now available for testing.