File: translate.pl

package info (click to toggle)
weechat-scripts 20071011
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 400 kB
  • ctags: 377
  • sloc: python: 3,470; perl: 1,305; ruby: 270; makefile: 37
file content (131 lines) | stat: -rw-r--r-- 3,914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#
# Copyright (c) 2006-2007 by FlashCode <flashcode@flashtux.org>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#

#
# Translate words and display result in infobar, local buffer or channel
#
# History:
# 2007-08-10, FlashCode <flashcode@flashtux.org>:
#     version 0.2: upgraded licence to GPL 3
# 2006-07-03, FlashCode <flashcode@flashtux.org>:
#     version 0.1: initial release
#

use strict;

my $version = "0.2";

# default values in setup file (~/.weechat/plugins.rc)
my $default_output_infobar = "on";
my $default_timeout = "2";

# script internal settings
my $languages = "de|el|en|es|fr|it|ja|ko|nl|pt|ru|zh|zt";

# init script
weechat::register("translate", $version, "", "Translation script");
weechat::set_plugin_config("output_infobar", $default_output_infobar) if (weechat::get_plugin_config("output_infobar") eq "");
weechat::set_plugin_config("timeout", $default_timeout) if (weechat::get_plugin_config("timeout") eq "");

# add command handlers for all languages
weechat::add_command_handler("translate", "translate", "translate text to other language",
                             "lang1 lang2 text [-o]",
                             "lang1: base language\n"
                             ."lang2: target language\n"
                             ." text: text to translate\n"
                             ."   -o: result is written on channel (visible by all)",
                             $languages." ".$languages);

# translate text with babelfish (altavista)
sub babelfish
{
    my $timeout = weechat::get_plugin_config("timeout");
    $timeout = $default_timeout if ($timeout eq "");
    
    if (`wget -q --timeout=$timeout --user-agent "Mozilla" --output-document=- "http://babelfish.altavista.com/babelfish/tr?lp=$_[0]_$_[1]&urltext=$_[2]"` =~ /.*<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div><\/td>/)
    {
        return $1;
    }
    return "";
}

# translate output
sub translate_output
{
    if ($_[1] == 1)
    {
        if (substr($_[0],0,1) eq "/")
        {
            weechat::command("/".$_[0], "", "");
        }
        else
        {
            weechat::command($_[0], "", "");
        }
    }
    else
    {
        my $output_infobar = weechat::get_plugin_config("output_infobar");
        $output_infobar = $default_output_infobar if ($output_infobar eq "");
        if ($output_infobar eq "on")
        {
            weechat::print_infobar(5, $_[0]);
        }
        else
        {
            weechat::print($_[0], "", "");
        }
    }
}

# /translate command
sub translate
{
    my $server = shift;
    my $args = shift;
    
    if ($args =~ /([a-zA-Z][a-zA-Z]) ([a-zA-Z][a-zA-Z]) (.*)/)
    {
        my $lang1 = $1;
        my $lang2 = $2;
        my $text = $3;
        
        # output on channel?
        my $output_chan = 0;
        if ($text =~ /(.*) -[oO]$/)
        {
            $output_chan = 1;
            $text = $1;
        }
        
        my $result = babelfish($lang1, $lang2, $text);
        if ($result eq "")
        {
            translate_output("Error: unable to translate (bad language or timeout)", 0);
        }
        else
        {
            translate_output($result, $output_chan);
        }
    }
    else
    {
        translate_output("Error: bad arguments", 0);
    }
    
    return weechat::PLUGIN_RC_OK;
}