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!;
49 # Here's how we recognize the language you're asking for. It looks
50 # like RTSL saves you a few keystrokes in #perl, huh?
51 $lang_regex = join '|', keys %lang_code;
55 return '' if $no_babel;
56 my ($from, $to, $phrase) = @_;
57 &main::DEBUG("babelfish($from, $to, $phrase)");
59 $from = $lang_code{$from};
60 $to = $lang_code{$to};
62 my $ua = new LWP::UserAgent;
63 $ua->proxy('http', $::param{'httpProxy'}) if (&::IsParam("httpProxy"));
65 $ua->agent("Mozilla/5.0 " . $ua->agent);
69 #HTTP::Request->new('POST', 'http://babelfish.altavista.com/raging/translate.dyn');
70 HTTP::Request->new('POST', 'http://babelfish.altavista.com/babelfish/tr');
72 # babelfish ignored this, but it SHOULD work
73 # Accept-Charset: iso-8859-1
74 # $req->header('Accept-Charset' => 'iso-8859-1');
75 # print $req->header('Accept-Charset');
76 $req->header('Accept-Language' => 'en');
77 $req->content_type('application/x-www-form-urlencoded');
79 return translate($phrase, "${from}_${to}", $req, $ua);
83 return '' if $no_babel;
84 my ($phrase, $languagepair, $req, $ua) = @_;
85 &main::DEBUG("translate($phrase, $languagepair, $req, $ua)");
87 my $urltext = uri_escape($phrase);
88 $req->content("urltext=$urltext&lp=$languagepair");
89 &main::DEBUG("http://babelfish.altavista.com/babelfish/tr??urltext=$urltext&lp=$languagepair");
91 my $res = $ua->request($req);
94 if ($res->is_success) {
95 my $html = $res->content;
96 # This method subject to change with the whims of Altavista's design
98 ($translated) = $html;
100 $translated =~ s/<[^>]*>//sg;
101 $translated =~ s/ / /sg;
102 $translated =~ s/\s+/ /sg;
103 #&main::DEBUG("$translated\n===remove <attributes>\n");
105 $translated =~ s/\s*Translate again.*//i;
106 &main::DEBUG("$translated\n===remove after 'Translate again'\n");
108 $translated =~ s/[^:]*?:\s*(Help\s*)?//s;
109 &main::DEBUG("$translated\n===remove to first ':', optional Help\n");
111 $translated =~ s/\n/ /g;
112 # FIXME: should we do unicode->iso (no. use utf8!)
114 $translated = ":("; # failure
116 &main::pSReply($translated);
121 #my $result = babel::babelfish('en','sp','hello world');
122 #my $result = babel::babelfish('en','sp','The cheese is old and moldy, where is the bathroom?');
123 my $result = babel::babelfish('en','gr','doesn\'t seem to translate things longer than 40 characters');
124 $result =~ s/; /\n/g;
125 print "Babelfish says: \"$result\"\n";