April 4, 2008

Language translation from the command line or clipboard

Author: Ben Martin

Web-based automatic translators such as translate.google.com are great for getting the gist of what a document is saying, but it can be cumbersome to have to open a new tab in your browser, load that URL, and copy and paste the text you want to translate into your browser. The twandgtw project allows you to get language translations directly from the Linux command line using either local dictionaries or online services.

Binary packages are available for Fedora 7 and 8. Other distributions have to follow the normal configure; make; sudo make install procedure to install from source.

As the project name suggests, twandgtw is actually two programs. tw is a non-graphical command-line word translator, while gtw has a graphical interface. To use tw to translate a word from English into Spanish, for example, specify a dictionary name and a word:

$ tw en-es hello
hello : hola

The gtw tool, shown in the figure, uses the GTK+ toolkit to present a graphical interface with similar functionality to the tw command line tool.

While the local dictionaries can only handle translating a single word at a time, you can feed online services a whole phrase at a time for translation. tw supports both www.freetranslation.com and translate.google.com for online translations. To translate a phrase you must pass it as a single argument to tw and a Web service:

$ tw translate.google.com.en-fr 'that looks like a tasty cake'
Qui ressemble à un gâteau savoureux


If you are a KDE3 user and you want to translate phrases frequently, a great place to gain access to this functionality is from the clipboard, with klipper. You can add translation as an action that klipper can perform for you, so you can translate text from any application. In the Configure Klipper window, under the Actions tab, create a regular expression with ^. as its contents. Right-click the regular expression and select Add Command. Set the command to kdialog --passivepopup "$(tw translate.google.com.en-fr "%s")" and give a description to the item. See the Global Shortcuts tab for the key to activate the "Manually Invoke Action on Current Clipboard" action. It can be handy to bind this action to a mouse button, especially if your mouse has five or more buttons.

Now you should be able to select a phrase and invoke a translation on it directly from the clipboard. The translated text will appear in a passive popup window that will close itself after 10 seconds. See this tutorial for other options for kdialog.

Having tw installed can make translating a phrase quick and easy by avoiding having to use a Web browser for the task. Since it's a command-line tool, it's also easy to integrate tw into other applications. And if you have a clipboard manager like klipper running, you can integrate the task of translation as an action you can perform on any text in the clipboard.


  • Tools & Utilities
  • Desktop Software
Click Here!