This is a (working) rough draft of a automatic translation script. Simply put, you choose to enable translation for incoming or outgoing text (or both), and pick a language to translate each to. Messages you send will be in the selected language, and messages received will be in the other selected language (if one or both of those are appropriately enabled). This script uses Google to make translations.
Simply type /translator or right click and choose "Translator" to open the simple dialog. For outgoing text, only standardly entered text and "/me" will be parsed.
I tried to iron out most of the errors, but at the rate they were popping up, it wouldn't surprise me if there were some left undiscovered. I still have a little bit to add too. Some parts of the code could definitely be written better, but hell, I was just happy that this worked.
Have fun with it, maybe you could find it useful.
;Original Author: Serpentsounds January 2010
alias urlencode return $regsubex($1-,/([^A-Z0-9])/gi,$+(%,$base($asc(\1),10,16)))
alias entdec return $replacex($$1,lt,<,gt,>,nbsp,$chr(160),amp,&,cent,$chr(162),pound,$chr(163),yen,$chr(165),euro,$chr(128),sect,$chr(167),copy,$chr(169),reg,$chr(174))
alias -l gtlangs {
var %l = Afrikaans Albanian Arabic Belarusian Bulgarian Catalan Chinese(Simplified) Chinese(Traditional) Croation Czech Danish Dutch English Estonian Filipino $&
Finnish French Galician German Greek Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian $&
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukranian Vietnamese Welsh Yiddish
return %l
}
alias -l gtabbrs return af sq ar be bg ca zh-CN zh-TW hr cs da nl en et tl fi fr gl de el iw hi hu is id ga it ja ko lv lt mk ms mt no fa pl pt ro ru sr sk sl es sw sv th tr uk vi cy yi
alias translator dialog $iif($dialog(translator),-v,-md) translator translator
alias tr.echo {
var %ticks = $1,%trans = $2-
var %nick = $hget(translator,$+(nick,%ticks)), %window = $hget(translator,$+(window,%ticks)), %action = $hget(translator,$+(action,%ticks))
var %pnick = $iif($left($nick(%window,%nick).pnick,1) isin ~&@%+,$+($v1,%nick),%nick)
if ($left(%window,1) == $chr(35)) var %msg = $iif(%action,* %pnick %trans,$+(<,$chr(3),$nick(%window,%nick).color,%pnick,$chr(3),>,$chr(32),%trans))
else var %msg = $iif(%action,* %nick %trans,$+(<,$chr(3),$cnick(nomode,1).color,%nick,$chr(3),>,$chr(32),%trans))
echo $iif(%action,$color(Action text),$color(Normal text)) -t %window %msg
window -g1 %window
}
alias tr.clrhsh {
var %ticks = $$1
if ($hget(translator,$+(action,%ticks))) hdel translator $+(action,%ticks)
hdel translator $+(window,%ticks)
hdel translator $+(transtext,%ticks)
hdel translator $+(nick,%ticks)
}
menu * {
-
Translator:translator
-
}
dialog translator {
size -1 -1 255 65
title "Translator"
option dbu
button "Close", 1, 210 50 37 12, cancel
check "", 2, 113 15 7 7
text "Enable auto translation:", 3, 10 15 60 10
box "Outgoing", 4, 5 5 120 40
text "Translate to:", 5, 10 30 40 10
combo 6, 60 28 60 20, drop vsbar
box "Incoming", 7, 130 5 120 40
text "Enable auto translation:", 8, 135 15 60 10
combo 9, 185 28 60 20, drop vsbar
check "", 10, 238 15 7 7
text "Translate to:", 11, 135 30 40 10
}
on *:DIALOG:translator:init:0:{
var %x = 1
while ($gettok($gtlangs,%x,32)) {
did -a translator 6,9 $v1
inc %x
}
if ($hget(translator,out)) did -c translator 2
if ($hget(translator,in)) did -c translator 10
if ($hget(translator,outl)) did -c translator 6 $findtok($gtlangs,$gettok($gtlangs,$findtok($gtabbrs,$v1,1,32),32),1,32)
if ($hget(translator,inl)) did -c translator 9 $findtok($gtlangs,$gettok($gtlangs,$findtok($gtabbrs,$v1,1,32),32),1,32)
}
on *:DIALOG:translator:sclick:2,10:hadd -m translator $replacex($did,2,out,10,in) $did($dname,$did).state
on *:DIALOG:translator:sclick:6,9:{
hadd -m translator outl $gettok($gtabbrs,$did($dname,6).sel,32)
hadd -m translator inl $gettok($gtabbrs,$did($dname,9).sel,32)
}
on *:INPUT:*:{
if ($hget(translator,out)) && (($left($1,1) != /) || ($1 == /me)) {
hadd -m translator ticks $ticks
if ($1 == /me) hadd -m translation $+(action,$hget(translator,ticks)) 1
transmsg $active $iif($hget(translator,$+(action,$hget(translator,ticks))),$2-,$1-)
halt
}
}
on ^*:TEXT:*:*:{
if ($hget(translator,in)) {
hadd -m translator ticks $ticks
hadd -m translator $+(nick,$hget(translator,ticks)) $nick
transecho $iif($chan,$v1,$nick) $1-
haltdef
}
}
on ^*:ACTION:*:*:{
if ($hget(translator,in)) {
hadd -m translator ticks $ticks
hadd -m translator $+(nick,$hget(translator,ticks)) $nick
hadd -m translator $+(action,$hget(translator,ticks)) 1
transecho $iif($chan,$v1,$nick) $1-
haltdef
}
}
alias transmsg {
hadd -m translator $+(window,$hget(translator,ticks)) $1
hadd -m translator $+(transtext,$hget(translator,ticks)) $2-
sockopen $+(googtout,$hget(translator,ticks)) ajax.googleapis.com 80
}
alias transecho {
hadd -m translator $+(window,$hget(translator,ticks)) $1
hadd -m translator $+(transtext,$hget(translator,ticks)) $2-
sockopen $+(googtin,$hget(translator,ticks)) ajax.googleapis.com 80
}
on *:SOCKOPEN:googtout*:{
sockwrite -n $sockname GET $+(/ajax/services/language/translate?v=1.0&q=,$urlencode($hget(translator,$+(transtext,$hget(translator,ticks)))),&langpair=|,$hget(translator,outl)) HTTP/1.1
sockwrite -n $sockname Host: ajax.googleapis.com $+ $str($crlf,2)
}
on *:SOCKOPEN:googtin*:{
sockwrite -n $sockname GET $+(/ajax/services/language/translate?v=1.0&q=,$urlencode($hget(translator,$+(transtext,$hget(translator,ticks)))),&langpair=|,$hget(translator,inl)) HTTP/1.1
sockwrite -n $sockname Host: ajax.googleapis.com $+ $str($crlf,2)
goto noerror
:error
noop $regex($sockname,([0-9]+))
var %ticks = $regml(1)
tr.echo %ticks $hget(translator,$+(transtext,%ticks))
tr.clrhsh %ticks
sockclose $sockname
reseterror
:noerror
}
on *:SOCKREAD:googtout*:{
var %text,%reg = "translatedText":"(.*?)","detectedSourceLanguage":
noop $regex($sockname,([0-9]+))
var %ticks = $regml(1)
sockread -f %text
if ($regex(%text,%reg)) {
var %unient = /\\u0026#([0-9]{1,4});/gi
$iif($hget(translator,$+(action,%ticks)),describe,msg) $hget(translator,$+(window,%ticks)) $regsubex($regml(1),%unient,$chr(\1))
if ($hget(translator,$+(action,%ticks))) hdel translator $+(action,%ticks)
hdel translator $+(window,%ticks)
hdel translator $+(transtext,%ticks)
sockclose $sockname
}
}
on *:SOCKREAD:googtin*:{
var %text,%reg = "translatedText":"(.*?)","detectedSourceLanguage":
noop $regex($sockname,([0-9]+))
var %ticks = $regml(1)
sockread -f %text
if ($regex(%text,%reg)) {
var %unient = /\\u0026#([0-9]{1,4});/gi,%trans = $regsubex($regml(1),%unient,$chr(\1))
var %unient = /\\u0026([a-z]+);/gi,%trans = $regsubex(%trans,%unient,$entdec(\1))
var %unient = /\\u([0-9a-f]{1,4})/gi,%trans = $regsubex(%trans,%unient,$chr($base(\1,16,10)))
tr.echo %ticks %trans
tr.clrhsh %ticks
sockclose $sockname
}
elseif ("could not reliably detect source language" isin %text) {
tr.echo %ticks $hget(translator,$+(transtext,%ticks))
tr.clrhsh %ticks
sockclose $sockname
}
}
The translator will the default action for all incoming text, and display the translated text in the same format once it is received from the socket. Because of this, your highlight seems to be prevented from ever activating. However, if it didn't do this, you would be seeing 2 of every message. You'd probably have to add a highlight exception within the translation script itself, or change your highlight management to be in a script further up on the list than the translator rather than the built-in highlight in the address book.
Works very good for me but I have an issue that i'm not not sure why it's happening..
When i open up the dialogue (right click in a channel) i then choose incoming, then i pick english and close the box.. it will translate perfectly.. my only issue is when i have it translating incoming messages my hightlights no longer seem to work.
Then when i turn the translation off my highlights are working just fine? What's causing this? Any info could help! I do love the script though! :)
^WeSt, the reason you received that error was that the socket wasn't closed properly before you attempted to restart its operation. You can perhaps add a check to be certain the socket is closed before it opens again:
if ($sock($+(googtout,$hget(translator,ticks))) { sockclose $v1 }
sockopen $+(googtout,$hget(translator,ticks)) ajax.googleapis.com 80
Serpentsounds, about the lag issue, you could minimize overhead and instruct the web server to keep the socket open by sending 'Connection: Keep-Alive' in your HTTP request. this way the connection will persist, allowing you to send subsequent requests without having to first establish a connection to Google's servers.
brilliant. just set the translator to operate on incoming and outgoing messages, open a query with yourself, and let the laughs flow:
http://up.k10x.net/sctzzylyyftqi/lol.jpg
the editbox contains what i typed to yield that translation :P
if you have trouble viewing certain characters, make sure you're using a recent version of mIRC (or enable UTF-8 display in font options) - and/or install additional Unicode fonts, such as those listed here: