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
19 eval "use URI::Escape"; # utility functions for encoding the
20 if ($@) { $no_babel++}; # babelfish request
21 eval "use LWP::UserAgent";
22 if ($@) { $no_babel++};
26 # Translate some feasible abbreviations into the ones babelfish
28 use vars qw!%lang_code $lang_regex!;
48 # Here's how we recognize the language you're asking for. It looks
49 # like RTSL saves you a few keystrokes in #perl, huh?
50 $lang_regex = join '|', keys %lang_code;
54 return '' if $no_babel;
55 my ($from, $to, $phrase) = @_;
56 &main::DEBUG("babelfish($from, $to, $phrase)");
58 $from = $lang_code{$from};
59 $to = $lang_code{$to};
61 my $ua = new LWP::UserAgent;
62 $ua->agent("Mozilla/4.5 " . $ua->agent); # Let's pretend
66 #HTTP::Request->new('POST', 'http://babelfish.altavista.com/raging/translate.dyn');
67 HTTP::Request->new('POST', 'http://babelfish.altavista.com/babelfish/tr');
69 # babelfish ignored this, but it SHOULD work
70 # Accept-Charset: iso-8859-1
71 # $req->header('Accept-Charset' => 'iso-8859-1');
72 # print $req->header('Accept-Charset');
73 $req->content_type('application/x-www-form-urlencoded');
75 return translate($phrase, "${from}_${to}", $req, $ua);
79 return '' if $no_babel;
80 my ($phrase, $languagepair, $req, $ua) = @_;
81 &main::DEBUG("translate($phrase, $languagepair, $req, $ua)");
83 my $urltext = uri_escape($phrase);
84 $req->content("urltext=$urltext&lp=$languagepair");
85 &main::DEBUG("http://babelfish.altavista.com/babelfish/tr??urltext=$urltext&lp=$languagepair");
87 my $res = $ua->request($req);
90 if ($res->is_success) {
91 my $html = $res->content;
92 # This method subject to change with the whims of Altavista's design
94 ($translated) = $html;
96 $translated =~ s/<[^>]*>//sg;
97 $translated =~ s/ / /sg;
98 $translated =~ s/\s+/ /sg;
99 #&main::DEBUG("$translated\n===remove <attributes>\n");
101 $translated =~ s/\s*Translate again.*//i;
102 &main::DEBUG("$translated\n===remove after 'Translate again'\n");
104 $translated =~ s/[^:]*?:\s*(Help\s*)?//s;
105 &main::DEBUG("$translated\n===remove to first ':', optional Help\n");
107 $translated =~ s/\n/ /g;
108 # FIXME should we do unicode->iso
110 $translated = ":("; # failure
112 &main::pSReply($translated);
117 #my $result = babel::babelfish('en','sp','hello world');
118 #my $result = babel::babelfish('en','sp','The cheese is old and moldy, where is the bathroom?');
119 my $result = babel::babelfish('en','gr','doesn\'t seem to translate things longer than 40 characters');
120 $result =~ s/; /\n/g;
121 print "Babelfish says: \"$result\"\n";