SMBs

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

can do these things.

shell> ./translate.pl license_en.txt > license_it.txt shell> cat license_it.txt ...

Quando parliamo di software libero, stiamo riferendosi alla libert�

per non valutare. Le nostre autorizzazioni del grande pubblico sono

progettate assicurarsi che avete la libert�per distribuire le copie

di software libero (ed addebitare questo servizio se voi desiderio),

che ricevete il codice sorgente o potete ottenerli se lo desiderate,

che potete cambiare il software o usare le parti di esso nei nuovi

programmi liberi; e quello sapete che potete fare queste

cose
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

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:

#!/usr/bin/perl

use strict; use warnings;

use WWW::Babelfish;

use Getopt::Long;

use IO::Prompt;

# map the language codes

my %language_code = (

    fr  => 'French',

    es  => 'Spanish',

    de  => 'German'

);

# parse command line arguments

my $translate_to_code;

my $result = GetOptions(

    'translate-to=s' => $translate_to_code,

);

if (not $result) {

    die "usage: ./translator.pl —translate-to fr | es | den";

}

if (not defined $translate_to_code) {

    die "usage: ./translator.pl —translate-to fr | es | den";

}

if ($translate_to_code !~ /(fr)|(es)|(de)/) {

    die "usage: ./translator.pl —translate-to fr | es | den";

}

# print instructions     

print "nn";

print "Interactive Language Translator.n";

print "Enter text to translate at prompt.n";

print "Type 'EXIT' to quit.nn";

# create the Babelfish service

warn "Connecting to Babelfish service ...n";

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

    service => 'Babelfish',

);

# check for errors

if (not defined $service) {

    die "Babelfish server unavailablen";

}

# loop while input is available

while ( prompt "text> " ) {

    chomp;

    # check if we need to exit

    if ($_ =~ /^EXIT$/i) {

        warn "Exiting ...n";

        exit;

    }

    # translate

    warn "Translating ...n";

    my $translated_text = $service->translate(

        source        => 'English',   # source language

        destination   => $language_code{$translate_to_code},   # destination language

        text          => $_           # text to translate

    );

    if (not defined $translated_text) {

        warn "Error while translating.n";

    }

    else {

        # output the translated text

        print "[" . uc $translate_to_code . "] $translated_textn";

    }

}

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.

Here's an example of a user session with this application:

$ ./translator.pl —translate-to es

Interactive Language Translator.

Enter text to translate at prompt.

Type 'EXIT' to quit.

Connecting to Babelfish service ...

text> The weather is hot today Translating ...

[ES] EL TIEMPO ES CALIENTE HOY

text> Maybe tomorrow it will be colder Translating ...

[ES] SERÁ QUIZÁ MAÑANA MÁS FRÍO

text> Babelfish's translation service is cool Translating ...

[ES] EL SERVICIO DE TRADUCCIÓN DE BABELFISH ESTÁ FRESCO

text> Goodbye Translating ...

[ES] ADIÓS

text> exit

Exiting ...

Now use your imagination to see what you can do with WWW::Babelfish.

Editor's Picks

Free Newsletters, In your Inbox