#!/usr/bin/perl

# translate.pl
#
# This program uses the web to translate stdin into another language,
# using Systran's webpage. The input is apparently limited up to 4096
# bytes.
#
# Usage: "translate.pl from to"
#       where from and two are each of:
#           de -- German                    ko -- Korean
#           en -- English                   nl -- Dutch
#           es -- Spanish                   pt -- Portuguese
#           fr -- French                    ru -- Russian
#           it -- Italian                   zh -- Simplified Chinese
#           ja -- Japanese                  zt -- Traditional Chinese
#
# However, beware that not all combinations of languages are
# available. Here are the currently published language combinations
# available (note: order of first/second langauage in the pair
# matters):
#
#       de -> en fr
#       en -> de es fr fr it ja ko nl pt ru zh zt
#       es -> en fr
#       fr -> de en en es it nl pt
#       it -> en fr
#       ja -> en
#       ko -> en
#       nl -> en fr
#       pt -> en fr
#       ru -> en
#       zh -> en
#       zt -> en
#
# This program then reads all the input until end-of-file (which you
# can signal from a command line shell with control-d). It then makes
# a web request, parses the results, and prints out the answer. If an
# unsupported translation is requested (e.g. ko -> pt), then the
# result is undefined (but most likely there will be no output).
#
# This program has the following dependencies:
#   - perl module URI::Escape
#       (http://search.cpan.org/~gaas/URI-1.35/URI/Escape.pm)
#   - wget 
#       (http://www.gnu.org/software/wget/wget.html)
#
# Updates:
#   - 2004/11/06: added default argument systran_lg=en, without which
#                 the translator was not returning results
#   - 2005/01/07: added stripping of leading/trailing whitespace in
#                 printed results
#   - 2005/07/28: removed some dead code changed from using a
#                 temporary file to receiving the data from wget
#                 directly
#
# Copyright (C) 2003  Greg Hamerly (greg_hamerly at baylor dot edu)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.  or see http://www.gnu.org/copyleft/gpl.html
#

use URI::Escape;
use strict;

my $from = "en";
my $to = "nl";

if (scalar(@ARGV) > 0) { $from = shift; }
if (scalar(@ARGV) > 0) { $to = shift; }

print(STDERR "Translate from $from -> $to. Type your message:\n");

my @lines = <STDIN>;
chomp(@lines);

my $text = uri_escape(join(' ', @lines));

my $script = "http://www.systranbox.com/systran/box";
my $default_args = "systran_id=SystranSoft-en&systran_lg=en&systran_charset=utf-8&ttype=text";
my $url = "$script?$default_args&systran_text=$text&systran_lp=$from\_$to";

@lines = `wget -q -O - "$url"`;
chomp(@lines);
my $on = 0;
my $translation;
foreach my $line (@lines) {
    if ($on) { $translation = $translation . "\n" . $line; }
    if ($line =~ /"translation"/) { $on = 1; $translation = $line; }
    if ($line =~ /<\/textarea>/)  { $on = 0; }
}

$translation =~ s/<[^>]+>//g; # strip HTML
$translation =~ s/^\W+|\W+$//g; # strip leading/trailing whitespace
print("$translation\n");

