1 # This program is copyright Jonathan Feinberg 1999.
2 # This program is distributed under the same terms as infobot.
6 # http://pobox.com/~jdf/
9 # First public release.
11 # hacked by Tim@Rikers.org to handle new URL and layout
17 my $url = 'http://babelfish.av.com/tr';
20 eval "use URI::Escape"; # utility functions for encoding the
21 if ($@) { $no_babelfish++}; # babelfish request
22 eval "use LWP::UserAgent";
23 if ($@) { $no_babelfish++};
27 # Translate some feasible abbreviations into the ones babelfish
29 use vars qw!%lang_code $lang_regex!;
52 # Here's how we recognize the language you're asking for. It looks
53 # like RTSL saves you a few keystrokes in #perl, huh?
54 $lang_regex = join '|', keys %lang_code;
58 return '' if $no_babelfish;
59 my ($from, $to, $phrase) = @_;
60 &::DEBUG("babelfish($from, $to, $phrase)");
62 $from = $lang_code{$from};
63 $to = $lang_code{$to};
65 my $ua = new LWP::UserAgent;
66 $ua->proxy('http', $::param{'httpProxy'}) if (&::IsParam("httpProxy"));
68 $ua->agent("Mozilla/5.0 " . $ua->agent);
71 my $req = HTTP::Request->new('POST', $url);
73 # babelfish ignored this, but it SHOULD work
74 # Accept-Charset: iso-8859-1
75 # $req->header('Accept-Charset' => 'iso-8859-1');
76 # print $req->header('Accept-Charset');
77 $req->header('Accept-Language' => 'en');
78 $req->content_type('application/x-www-form-urlencoded');
80 return translate($phrase, "${from}_${to}", $req, $ua);
84 return '' if $no_babelfish;
85 my ($phrase, $languagepair, $req, $ua) = @_;
86 &::DEBUG("translate($phrase, $languagepair, $req, $ua)");
88 my $trtext = uri_escape($phrase);
89 $req->content("trtext=$trtext&lp=$languagepair");
90 &::DEBUG("$url??trtext=$trtext&lp=$languagepair");
92 my $res = $ua->request($req);
95 if ($res->is_success) {
96 my $html = $res->content;
97 # This method subject to change with the whims of Altavista's design
99 ($translated) = $html;
101 $translated =~ s/<[^>]*>//sg;
102 $translated =~ s/ / /sg;
103 $translated =~ s/\s+/ /sg;
104 #&::DEBUG("$translated\n===remove <attributes>\n");
106 $translated =~ s/\s*Translate again.*//i;
107 &::DEBUG("$translated\n===remove after 'Translate again'\n");
109 $translated =~ s/[^:]*?:\s*(Help\s*)?//s;
110 &::DEBUG("len=" . length($translated) . " $translated\n===remove to first ':', optional Help\n");
112 $translated =~ s/\n/ /g;
113 # FIXME: should we do unicode->iso (no. use utf8!)
115 $translated = ":("; # failure
117 $translated = "babelfish.pl: result too long, probably an error" if (length($translated) > 700);
124 my $babel_lang_regex = "de|ge|gr|el|sp|es|en|fr|it|ja|jp|ko|kr|nl|po|pt|ru|zh|zt";
126 ($babel_lang_regex)\w* # from language?
128 ($babel_lang_regex)\w* # to language?
130 (.+) # The phrase to be translated
132 &::performStrictReply(&babelfishParam(lc $1, lc $2, lc $3));
139 #my $result = babelfish::babelfish('en sp hello world');
140 #my $result = babelfish::babelfish('en sp The cheese is old and moldy, where is the bathroom?');
141 my $result = babelfish::babelfish('en gr doesn\'t seem to translate things longer than 40 characters');
142 $result =~ s/; /\n/g;
143 print "Babelfish says: \"$result\"\n";