How do I... Perform translations with Perl and Babel Fish?

You can add real-time translation to your Perl application through a neat CPAN module called WWW::Babelfish. This article shows how you can use WWW::Babelfish to translate text between 15+ language pairs and create an interactive translation tool for ad-hoc text strings.

How do I...Real-time online translation services such as AltaVista Babel Fish and Google Translate make it easier for users to translate Web pages into their native languages. These services stimulate information flow and help people of different cultures engage with each other by improving the accessibility of blogs, newsgroups, and other Web content written in languages other than English.

You can add real-time translation to your Perl application through a neat CPAN module called WWW::Babelfish. This article shows how you can use WWW::Babelfish to translate text between 15+ language pairs and create an interactive translation tool for ad-hoc text strings.

Translating strings

Download the WWW::Babelfish module and install it to your Perl development environment by following the instructions supplied with the module.

(Note that usage of the Babel Fish translation service is subject to certain terms and conditions — e.g., there is a limit on how much can be translated in a single session— so you should always check with the service providers that your intended usage via WWW::Babelfish conforms to those terms and conditions.)

Once the module is installed, take it for a spin by typing out the following starter script:

#!/usr/bin/perl

use strict; use warnings;

use WWW::Babelfish;

# text to translate

my $text_en = 'Hello, my name is Maurice';

# create the Babelfish service

my $service = WWW::Babelfish->new(

service => 'Babelfish',

);

# check for errors

if (not defined $service) {

die "Babelfish server unavailablen";

}

# show text to be translated

print "[EN] $text_enn";

# translate to French

my $text_fr = $service->translate(

source => 'English', # source language

destination => 'French', # destination language

text => $text_en, # text to translate

);

if (not defined $text_fr) {

print "Error while translating to Frenchn";

}

else {

print "[FR] $text_frn";

}

This script translates an English sentence into French using AltaVista Babel Fish. It begins by creating a WWW::Babelfish object; then the new() method attempts to connect to the Babel Fish server and returns undef if there is an error while connecting. (In this example, the object's constructor requires a service parameter, which is set to 'Babelfish' in this case.)

Once a connection is established, the object's translate() method performs the actual translation. This method accepts three named parameters: source, which represents the source language of the text; destination, which represents the language to translate to; and text, which represents the text to translate. If successful, the translate() method returns the translated text or undef on error.

Here's the output of the script:

[EN] Hello, my name is Maurice
[FR] Bonjour, mon nom est Maurice

This revision of the previous script translates a single English sentence into French, Spanish, and German:

#!/usr/bin/perl

use strict; use warnings;

use WWW::Babelfish;

# text to translate

my $text_en = 'Hello, my name is Maurice';

# create the Babelfish service

my $service = WWW::Babelfish->new(

service => 'Babelfish',

);

# check for errors

if (not defined $service) {

die "Babelfish server unavailablen";

}

# show text to be translated

print "[EN] $text_enn";

# translate to French

my $text_fr = $service->translate(

source => 'English', # source language

destination => 'French', # destination language

text => $text_en # text to translate

);

if (not defined $text_fr) {

print "Error while translating to Frenchn";

}

else {

print "[FR] $text_frn";

}

# translate to Spanish

my $text_es = $service->translate(

source => 'English', # source language

destination => 'Spanish', # destination language

text => $text_en # text to translate

);

if (not defined $text_es) {

print "Error while translating to Spanishn";

}

else {

print "[ES] $text_esn";

}

# translate to German

my $text_de = $service->translate(

source => 'English', # source language

destination => 'German', # destination language

text => $text_en # text to translate

);

if (not defined $text_de) {

print "Error while translating to Germann";

}

else {

print "[DE] $text_den";

}

This script creates a new WWW::Babelfish object and then uses that object's translate() method to perform translation in different languages. Here's the output:

[EN] Hello, my name is Maurice

[FR] Bonjour, mon nom est Maurice

[ES] Hola, mi nombre es Maurice

[DE] Hallo, ist mein Name Maurice

Note: The WWW::Babelfish module also supports the Google Translate service.

Translating files

In addition to translating strings, WWW::Babelfish also supports translating text files and writing the translated contents to a new file. The following example illustrates how this works:

#!/usr/bin/perl

use strict; use warnings;

use WWW::Babelfish;

# input file to translate

# specified as command line argument

my $input_file = shift;

if (not defined $input_file) {

die "usage: ./translate.pl file_to_translate.txtn";

}

# create the Babelfish service

my $service = WWW::Babelfish->new(

service => 'Babelfish',

);

# check for errors

if (not defined $service) {

die "Babelfish server unavailablen";

}

# open input file to be translated

open my $ifh, '<', $input_file

or die "Could not open $input_file for reading: $!n";

# translate

# write translation to output file handle

my $ret = $service->translate(

source => 'English', # source language

destination => 'Italian', # destination language

text => $ifh, # file handle to read from

ofh => *STDOUT # file handle to write to

);

if (not defined $ret) {

print "Error while translating to Italiann";

}

This script accepts a single command-line argument: the full path to the source text file to be translated (which, in this example, is assumed to be in English). Once this argument is received, a connection is opened to the Babel Fish server, and a file handle is opened to the source file. The translate() method is called, but this time the argument passed to the text parameter is not a string but the file handle to the source file.

In this script, an additional parameter called ofh, which specifies the output file handle, is passed to the translate() method. Once the source file is transmitted and translated, the resulting translation is written to this handle. In this example, the translated text is written to STDOUT, but you could just as easily specify a disk file.

Here's an example of how this could be used (the output is truncated for brevity):

shell> cat license_en.txt

...

When we speak of free software, we are referring to freedom, not price. Our

General Public Licenses are designed to make sure that you have the freedom to

distribute copies of free software (and charge for this service if you wish),

that you receive source code or can get it if you want it, that you can change

the software or use pieces of it in new free programs; and that you know you

Note: WWW::Babelfish can also translate to non-Latin character sets, such as simplified Chinese. The output will contain UTF-8 characters, and it can be viewed in any Xterm that supports the UTF-8 character set. Figure A illustrates the output of the previous listing when translated to simplified Chinese.
Figure A

Interactive translation: A real-world application

WWW::Babelfish makes it possible to work with a variety of ingenuous applications. One of the most interesting uses is interactive language translation, which has users typing in phrases and receiving real-time translation of those phrases in the target language.

It's fairly easy to construct such a translator in Perl. Here's the code:

This script acts as an interactive translation service, translating everything you enter at the input prompt to the target language. This target language must be specified when you first run the script on the command line.

The script internally maps each language name to the corresponding two-letter international language code. This particular listing supports French (fr), Spanish (es), and German (de), but you can add as many languages as you like. Next, the Getopt::Long module is used to parse the command line and figure out which language the translator should translate to. The script prints a welcome header and usage instructions and then opens a connection to the Babel Fish service.

A while() loop is used to fetch input text from STDIN, transmit this text to the Babel Fish service, and display the translated result. This loop will run as long as the user does not input the string EXIT or exit; once this termination string is encountered, the loop is broken and the program is ended.