~ubuntu-branches/ubuntu/saucy/amsn/saucy

« back to all changes in this revision

Viewing changes to plugins/Translate/translate.tcl

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2010-04-13 23:21:29 UTC
  • mfrom: (1.1.11 upstream) (3.1.8 sid)
  • Revision ID: james.westby@ubuntu.com-20100413232129-vgpx20brdd2qavs7
Tags: 0.98.3-0ubuntu1
* Merge from Debian unstable (LP: #449072), remaining Ubuntu changes:
  - add 08_use_aplay_for_sound.dpatch patch by Festor Wailon Dacoba to use
    aplay to play sounds
  + debian/control:
    - modify iceweasel to firefox | abrowser in amsn Suggests field
    - add xdg-utils and gstreamer0.10-nice to amsn Depends field
    - mofify sox to alsa-utils in amsn Suggests field as we are now using
      aplay
* New upstream release (LP: #562619), tarball repacked according to
  debian/README.source.
* Fix missing-debian-source-format lintian warning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Translate 0.3 for aMSN 0.97 - by number-g (g-at-imagination-dot-eu-dot-org)
 
2
 
 
3
package require http
 
4
 
 
5
namespace eval ::translate {
 
6
 
 
7
        array set ::translate::languages {
 
8
                Albanian sq Arabic ar Bulgarian bg Chinese_simp zh-cn
 
9
                Chinese_trad zh-tw Catalan ca Croatian hr Czech cs
 
10
                Danish da Dutch nl English en Estonian et Filipino tl
 
11
                Finnish fi French fr Galician gl German de Greek el
 
12
                Hebrew iw Hindi hi Hungarian hu Indonesian id Italian it
 
13
                Japanese ja Korean ko Latvian lv Lithuanian lt Maltese mt
 
14
                Norwegian no Polish pl Portuguese pt Romanian ro Russian ru
 
15
                Spanish es Serbian sr Slovak sk Slovenian sl Swedish sv
 
16
                Thai th Turkish tr Ukrainian uk Vietnamese vi
 
17
        }
 
18
 
 
19
        proc init {dir} {
 
20
                ::plugins::RegisterPlugin "Translate"
 
21
                ::plugins::RegisterEvent "Translate" new_conversation populate_chatwindow
 
22
                ::plugins::RegisterEvent "Translate" chat_msg_send outgoing
 
23
                ::plugins::RegisterEvent "Translate" chat_msg_receive incoming
 
24
 
 
25
                # Set config variables per-chatid.
 
26
                set c [::abook::getAllContacts]
 
27
 
 
28
                foreach contact $c {                            
 
29
                        set ::translate::config($contact) 0
 
30
                        set ::translate::config($contact.from) Source
 
31
                        set ::translate::config($contact.to) Destination
 
32
                        set ::translate::config($contact.hide) 0
 
33
                        set ::translate::config($contact.viceversa) 0
 
34
                }
 
35
        }
 
36
 
 
37
        # Shameless self-promotion
 
38
        set ::translate::configlist [list \
 
39
                                         [list label "Translate 0.3 for aMSN 0.97"] \
 
40
                                         [list label "by number-g (g-at-imagination-dot-eu-dot-org)"] \
 
41
                                         [list label ""] \
 
42
                                         [list label "Hint: starting your sentences with a \".\" will"] \
 
43
                                         [list label "bypass the translator."] \
 
44
                                         [list label ""] \
 
45
                                         [list label "If you find this plugin useful, please consider"] \
 
46
                                         [list label "making a donation by clicking the link below:"] \
 
47
                                         [list label ""] \
 
48
                                         [list frame ::translate::donatebutton ""] \
 
49
                                         [list label ""]
 
50
                                    ]
 
51
 
 
52
        proc donatebutton {win} {
 
53
 
 
54
                set frame $win.frame
 
55
                frame $frame
 
56
 
 
57
                label $frame.donate -text "Donate!" -cursor hand2 -font splainf \
 
58
                    -background [::skin::getKey extrastdwindowcolor] -foreground [::skin::getKey extralinkcolor]
 
59
                
 
60
                bind $frame.donate <Enter> "$frame.donate configure -font sunderf -cursor hand2 \
 
61
        -background [::skin::getKey extralinkbgcoloractive] -foreground [::skin::getKey extralinkcoloractive]"
 
62
                bind $frame.donate <Leave> "$frame.donate configure -font splainf -cursor left_ptr \
 
63
        -background [::skin::getKey extrastdwindowcolor] -foreground [::skin::getKey extralinkcolor]"
 
64
                bind $frame.donate <ButtonRelease> "launch_browser https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=SKCWYSL36RGZW&lc=GB&item_name=Translate_plugin_v0.3-for_aMSN_0.97&currency_code=GBP&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"
 
65
                
 
66
                pack $frame.donate
 
67
                pack $frame
 
68
        }
 
69
 
 
70
        proc populate_chatwindow {event evpar} {
 
71
 
 
72
                upvar 2 $evpar args
 
73
 
 
74
                set chatid $args(chatid)
 
75
                set window [::ChatWindow::For $chatid] 
 
76
                
 
77
                set placement $window.top.padding
 
78
                pack [create_ui $window $placement $chatid] -fill x
 
79
        }
 
80
 
 
81
        proc create_ui {window placement chatid} {
 
82
                
 
83
                set ui $placement.translate
 
84
                frame $ui
 
85
 
 
86
                set onoff $ui.cb
 
87
                set viceversa $ui.viceversa
 
88
                set showhide $ui.showhide
 
89
 
 
90
                set from $ui.from
 
91
                set to  $ui.to
 
92
                set fl  $ui.fl
 
93
                set tl  $ui.tl
 
94
                label $fl -text "Translate from: "
 
95
                label $tl -text "To:"
 
96
                
 
97
                checkbutton $onoff -indicatoron 1 -onvalue 1 -offvalue 0        \
 
98
                    -selectcolor green -text "On/Off" -variable ::translate::config($chatid)
 
99
 
 
100
                checkbutton $viceversa -indicatoron 1 -onvalue 1 -offvalue 0        \
 
101
                    -selectcolor green -text "Translate incoming messages" -variable ::translate::config($chatid.viceversa)
 
102
 
 
103
                checkbutton $showhide -indicatoron 1 -onvalue 1 -offvalue 0        \
 
104
                    -selectcolor green -text "Hide original" -variable ::translate::config($chatid.hide)
 
105
 
 
106
 
 
107
                combobox::combobox $from -editable 0 -width 11 -textvariable ::translate::config($chatid.from)
 
108
                combobox::combobox $to -editable 0 -width 11 -textvariable ::translate::config($chatid.to)
 
109
 
 
110
                # Populate comboboxes.
 
111
                foreach language [lsort [array names ::translate::languages]] {
 
112
                        $from list insert end $language
 
113
                        $to list insert end $language   
 
114
                }
 
115
 
 
116
                pack $fl -side left;                            pack $from -side left
 
117
                pack $tl -side left -padx 5;    pack $to -side left
 
118
                
 
119
                pack $onoff -side right -padx 2
 
120
                pack $showhide -side right -padx 2
 
121
                pack $viceversa -side right -padx 2
 
122
 
 
123
                return $ui      
 
124
        }
 
125
 
 
126
        proc outgoing {event evpar} {
 
127
 
 
128
                upvar 2 chatid chatid
 
129
                upvar 2 msg msg
 
130
 
 
131
                set from  $::translate::languages($::translate::config($chatid.from))
 
132
                set to    $::translate::languages($::translate::config($chatid.to))
 
133
                set state $::translate::config($chatid)
 
134
 
 
135
                if {$state==1} {
 
136
                        set msg [translate $chatid $msg $from $to]
 
137
                } else {
 
138
                        return 1
 
139
                }
 
140
                
 
141
        }
 
142
 
 
143
        proc incoming {event evpar} {
 
144
                upvar 2 user user
 
145
                upvar 2 chatid chatid
 
146
                upvar 2 message message
 
147
 
 
148
                if {$user!=[::config::getKey login]}  {
 
149
                        
 
150
                        if {$::translate::config($chatid.viceversa)==1} {
 
151
                                
 
152
                                set from  $::translate::languages($::translate::config($chatid.from))
 
153
                                set to    $::translate::languages($::translate::config($chatid.to))
 
154
                                set state    $::translate::config($chatid)
 
155
 
 
156
                                if {$state==1} {
 
157
                                        set message [translate $chatid $message $to $from]
 
158
                                } else {
 
159
                                        return 1
 
160
                                } 
 
161
                        } else {
 
162
                                return 1
 
163
                        }
 
164
                }
 
165
        }
 
166
        
 
167
 
 
168
        proc translate {chatid msg from to} {
 
169
 
 
170
                set case [testcase $msg]
 
171
 
 
172
                # A "." at the beginning of a line bypasses translation
 
173
                if {[string range $msg 0 0]!="."} {
 
174
 
 
175
                        set original ""
 
176
                        set query [::http::formatQuery v 1.0 q [convert $msg] langpair $from|$to] ;# See convert proc for explaination.
 
177
 
 
178
                        # Include original text, but we don't need to see the emoticons twice.
 
179
                        if {$translate::config($chatid.hide)==0} {
 
180
                                set original "« $msg »\n- "
 
181
                                set query [::http::formatQuery v 1.0 q [convert [stripemoticons $msg]] langpair $from|$to]
 
182
                        }
 
183
 
 
184
                        set tran [::http::geturl http://ajax.googleapis.com/ajax/services/language/translate?$query -query]
 
185
                        set tran [::http::data $tran]
 
186
 
 
187
                        set tran [split $tran \"]                                                                       ;# Maybe there is a more elegant way to do this(?) 
 
188
                        set tran [string trim [replace [lindex $tran 5]]]       ;# See replace proc for explaination
 
189
 
 
190
 
 
191
                        # Some stuff here to make things feel more natural and avoid needless repetition:
 
192
                        # 1: Don't bother with empty strings or dupes.
 
193
                        # 2: Google will add a space to (for example) "hi...", returning "hi ..." adding needless repetition to the output.
 
194
                        # 3: Can't remember exactly why these are here, but I can remember that it solved something that irritated me.
 
195
 
 
196
                        if {$tran!=$msg && $tran!="" \
 
197
                                && [regsub -all { } $tran {}]!=[stripemoticons $msg] \
 
198
                                && [stripemoticons $msg]!=$tran \
 
199
                                && [string tolower $tran]!=$msg} {
 
200
                                
 
201
                                set msg $original$tran
 
202
                        }
 
203
                        # If I choose to type in lower case, I want the translated text to reflect this:
 
204
                        if {$case==0} {return [string tolower $msg]} else {return $msg}
 
205
                } else { 
 
206
                        return 1
 
207
                }
 
208
        }
 
209
 
 
210
 
 
211
        # Google returns these as HTML escaped unicode characters. We want them to be legible:
 
212
 
 
213
        proc replace {text} {
 
214
                array set chars {
 
215
 
 
216
                        u0026ndash;                     – 
 
217
                        u0026mdash;                     —       
 
218
                        u0026iexcl;                     ¡       
 
219
                        u0026iquest;            ¿       
 
220
                        u0026quot;                      \"
 
221
                        u0026ldquo;                     “
 
222
                        u0026rdquo;                     ”       
 
223
                        u0026lsquo;                     ‘
 
224
                        u0026rsquo;                     ’
 
225
                        u0026aquo;                      «
 
226
                        u0026raquo;                     »
 
227
                        u0026amp;                       \\&
 
228
                        u0026cent;                      ¢
 
229
                        u0026copy;                      ©
 
230
                        u0026divide;            ÷ 
 
231
                        u0026gt;                                > 
 
232
                        u0026lt;                                < 
 
233
                        u0026micro;                     µ 
 
234
                        u0026middot;            ·
 
235
                        u0026para;                      ¶
 
236
                        u0026plusmn;            ±  
 
237
                        u0026euro;                      €  
 
238
                        u0026pound;                     £ 
 
239
                        u0026reg;                       ® 
 
240
                        u0026sect;                      §  
 
241
                        u0026trade;                     ™
 
242
                        u0026yen;                       ¥ 
 
243
                        u0026#39;                       '
 
244
                        u003d                                   =
 
245
                }
 
246
                
 
247
                foreach {char replace} [array get chars] {
 
248
                        regsub -all \\\\$char $text $replace text
 
249
                }
 
250
                return $text
 
251
        }
 
252
 
 
253
        proc stripemoticons {text} {
 
254
 
 
255
                set emoticons [list {:-\)} {:\)} {:-D} {:d} {:-O} {:o} {:\-P} {:p} {;-\)} {;\)} {:-\(} {:\(} {:-S} {:s} {:-\|} {:\|}  \
 
256
                                   {:'\(} {:-\$} {:\$} {\(H\)} {:-@} {:@} {\(A\)} {\(6\)} {:-#} {8o\|} {8-\|} {\^o\)} {:-\*} {\+o\(}            \
 
257
                                   {:\^\)} {\*-\)} {<:o\)} {8-\)} {\|-\)} {\(C\)} {\(Y\)} {\(N\)} {\(B\)} {\(D\)} {\(X\)} {\(Z\)}               \
 
258
                                   {\(\{\)} {\(\}\)} {:-\[} {:\[} {\(\^\)} {\(L\)} {\(U\)} {\(K\)} {\(G\)} {\(F\)} {\(W\)} {\(P\)}              \
 
259
                                   {\(~\)} {\(@\)} {\(&\)} {\(T\)} {\(I\)} {\(8\)} {\(S\)} {\(\*\)} {\(E\)} {\(O\)} {\(M\)} {\(sn\)}            \
 
260
                                   {\(bah\)} {\(pl\)} {\(\|\|\)} {\(pi\)} {\(so\)} {\(au\)} {\(ap\)} {\(um\)} {\(ip\)} {\(co\)}                 \
 
261
                                   {\(mp\)} {\(st\)} {\(li\)} {\(mo\)} ]
 
262
 
 
263
                foreach code $emoticons {
 
264
                        regsub -all -nocase $code $text {} text
 
265
                }
 
266
                return [string trim $text]
 
267
        }
 
268
 
 
269
        # Find out if we are typing in lowercase:
 
270
        proc testcase {string} {
 
271
 
 
272
                set x [string length $string]
 
273
                for {set i 0} {$i<$x} {incr i} {
 
274
                        if {[string is alpha [string range $string $i $i]]} {append output [string range $string $i $i]}
 
275
                }
 
276
                if {[string is lower $output]} {return 0} else {return 1}
 
277
        }
 
278
 
 
279
 
 
280
        # Sending the text to Google as-is caused problems with accents for Windows users;
 
281
        # ie - "ça va?" was being sent as "a va?", leading to (sometimes hilarious) mistranslations.
 
282
        #
 
283
        # This proc converts each character to HTML escaped unicode before sending to Google.
 
284
        proc convert {string} {
 
285
 
 
286
                set x [string length $string]
 
287
                for {set i 0} {$i<$x} {incr i} {
 
288
                        append output "&#[scan [string range $string $i $i] %c];"
 
289
                }
 
290
                return $output
 
291
        }
 
292
}