http://qs321.pair.com?node_id=182303
Category: IRC
Author/Contact Info shockme
Description: This script allows you to send a message in (almost) any language, and translate any responses. It uses the Lingua::Translate module (Babelfish) to accomplish the translation, so while you're not likely to come off as a linguistic master, you won't be restricted to "no hablo espanol" either.
#!/usr/bin/perl -w
#
#  translate - Xchat Translation Script by shock
#  Site: http://www.exitwound.org
#  Email: shock@exitwound.org
#
# INSTALL:  You MUST install the Lingua::Translate module 
# before using this script.  See http://search.cpan.org to
# locate it.  
#
# The $DEFAULT variable should be set to your 2- or 3- 
# character language code (i.e., en for English, es for
# Espanol, etc.).  See below for how to locate other 
# character codes.
#
# Place translate.pl into your /home/<username>/.xchat/ 
# directory and restart xchat.  
#
#           OR
#
# Place translate.pl into your /home/<username>/.xchat/
# directory and type 
# "/load home/<username>/.xchat/translate.pl" into the
# xchat window.
#
# You will see "translate 0.01 by shock has been 
# loaded" if the script loads properly. 
#
# commands: Help screen            = /translate
#           XX to Default Language = /pr XX::message
#           Default Language to XX = /tr XX::message
#
# Where XX is an acceptible language code as specified at
# http://search.cpan.org/doc/JHI/perl-5.8.0-RC1/lib/I18N/LangTags/List
+.pm 
#
# If XX is not specified, the system defaults to Espanol.
#
# examples:
#    Assume you want to send a message in Spanish:
#
#    /tr es::this is a test message
#
#    Assume you have received a message in German:
#
#    /pr de::dieses ist eine Testanzeige
#
# The /tr commands will post the message to the chatroom.
# The /pr commands will only echo the translation to you.
# No one in the chatroom will see it.
#
#
# note 1: The Lingua::Translate module uses Babelfish
#   (http://babelfish.altavista.com) for it's translations.
#   So, although this module will allow you to communicate
#   in other languages, you're not going to to be the most
#   proficient of speakers.  This module does NOT provide
#   (nor does it intend to) an ideal solution.  It does,
#   however, allow you to somewhat break the language
#   barrier.
#
# note 2: Because the script has to contact Babelfish, 
#    when you hit the enter key, you will notice a delay.
#    Sometimes it's a short delay, sometimes it's a long
#    delay.  It all depends upon Internet traffic.  
#    That's just the way it is, baby.
#
# ack: Thanks to BrainRawt (http://rawt.daemon.sh) for
#    writing Xchat scripts.  I've used his scripts as a
#    go-by for this one.

##################################
#       DEFAULT LANGUAGE         #
##################################
#        CHANGE THIS             #
#     TO YOUR LANGUAGE           #
#                                #
# Common choices:                #
# English = en                   #
# Espanol = es                   #
# German  = de                   #
# Italian = it                   #
##################################
$DEFAULT = "en";

##################################
#         VERSION NUMBER         #
##################################
$VER = "0.01";

IRC::register("translate", "$VER", "", "");
IRC::print("\0034translate $VER by shock has been loaded\003");
IRC::add_command_handler("translate", "help_handler");
IRC::add_command_handler("tr", "translation_handler");
IRC::add_command_handler("pr", "private_handler");
use Lingua::Translate;

##################################
#         HELP SCREEN            #
##################################
sub help_handler {
    IRC::command("/echo --------------------------");
    IRC::command("/echo translate $VER help screen:");
    IRC::command("/echo --------------------------");
    IRC::command("/echo /tr XX::message - translates message from $DEF
+AULT to XX in channel.");
    IRC::command("/echo /pr XX::message - from XX to $DEFAULT; Respons
+e is echoed to user.");
    IRC::command("/echo --------------------------");
    IRC::command("/echo XX is a two-character language tag.");
    IRC::command("/echo Espanol is the default if XX is not specified.
+");
    IRC::command("/echo See http://search.cpan.org/doc/JHI/perl-5.8.0-
+RC1/lib/I18N/LangTags/List.pm for a list of acceptible language tags.
+");
    IRC::command("/echo --------------------------");
    return 1;
}

##################################
#      DEFAULT TRANSLATION       #
#   From XX to User's Default    #
#           Language             #
##################################
sub private_handler {
    my $language = shift(@_);
    if ($language =~ /(\w\w)::/) {
    ($src, $message) = split(/::/, $language);
    }
    else {
    $src = "es";
    $message = $language;
    }
    my $xl8r = Lingua::Translate->new(src => "$src",
                                      dest => "$DEFAULT")
        or die "No translation server available for $src -> en";
    my $english = $xl8r->translate($message); # dies or croaks on erro
+r

    IRC::command("/echo $english");
    return 1; 
}

##################################
#   USER-SPECIFIED TRANSLATION   #
#      From User's Default       #
#        Language to XX          #
##################################
sub translation_handler {
    my $language = shift(@_);
    if ($language =~ /(\w\w)::/) {
    ($dest, $message) = split(/::/, $language);
    }
    else {
    $dest = "es";
    $message = $language;
    }
    my $xl8r = Lingua::Translate->new(src => "$DEFAULT",
                                      dest => "$dest")
        or die "No translation server available for en -> $dest";
    my $translation = $xl8r->translate($message); # dies or croaks on 
+error

    IRC::command("$translation");
    return 1; 
}