Auto Translator

By Serpentsounds on Jan 24, 2010

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
  }
}

Comments

Sign in to comment.
rabo3   -  Sep 24, 2016

it's not working with me, :(

 Respond  
Serpentsounds   -  May 14, 2011

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.

 Respond  
Flamey   -  May 11, 2011

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! :)

 Respond  
troll   -  Apr 21, 2011

works fine for me .

 Respond  
Serpentsounds   -  Feb 08, 2011

I'm guessing that happened since he pasted several lines at once, and the socket names are based on $ticks. Another thing to fix if I ever come back to this. :p

 Respond  
Jethro   -  Feb 05, 2011

^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
 Respond  
^WeSt   -  Jan 01, 2011

good but has an error :

  • /sockopen: 'googtout50685429' socket in use (line 302, test.mrc)
  • /sockopen: 'googtout50653543' socket in use (line 302, test.mrc)
 Respond  
TMFKSOFT   -  Jan 01, 2011

how cna i make it show text before the translation e.g.

Hola is shown as Hello [Spannish] or just Hello [Translated] Please tell me :)
 Respond  
blackvenomm666   -  Nov 29, 2010

exactly cause i was on a site wanting to use the translator but had no clue wtf they were speaking haha

 Respond  
Serpentsounds   -  Nov 29, 2010

Yeah blackvenom that is something that I was also considering: have an optional prefix/suffix to the message that says [Translated from somelanguage]. Is that what you mean?

 Respond  
blackvenomm666   -  Nov 29, 2010

ok idea maybe have it tell you what language its translating from with the incoming text translation

 Respond  
jaytea   -  Nov 29, 2010

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.

 Respond  
jaytea   -  Nov 29, 2010

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:

http://en.wikipedia.org/wiki/Unicode_typeface

 Respond  
blackvenomm666   -  Nov 28, 2010

ahh ok

 Respond  
Serpentsounds   -  Nov 28, 2010

That's an issue with your local machine, I'm afraid to say. If you don't have the support to display some languages (such as Japanese which uses a specific set of unicode characters) you will get boxes.

 Respond  
blackvenomm666   -  Nov 28, 2010

just checked some mroe out the japanese language doesnt work it returns as boxes

 Respond  
Serpentsounds   -  Nov 28, 2010

Thanks blackvenom, glad you found some use. :)

...Hey look above, it's me commenting in the summer about how I was going to upgrade it! And I still haven't. <_<

Also, fixing that color issue does sound like something important. I'll address that when I fix this up.

 Respond  
blackvenomm666   -  Nov 28, 2010

serpent this is a very very nice translator been wanting one for gaelic/irish very good job works great. maybe you could have it strip out the color code numbers? cause they show up before text example 6,11<<< dont gur mhaith leat é a
<+Xombie> 10,1<<< baint ~ ~

 Respond  
Serpentsounds   -  Jul 19, 2010

No problem! Haven't got a comment on this in a while. You reminded me, I was going to upgrade it. I should get around to that. ;]

 Respond  
TacticalThunder   -  Jul 05, 2010

Serpentsounds, Thank you, I've been looking all over the place to find a translator, and this one works perfect for me. Thanks

 Respond  
Serpentsounds   -  Feb 17, 2010

Sorry to hear that you're having trouble with it. Keep in mind that this is constantly opening sockets, so if your resources and/or bandwidth are limited (or just have a faulty connection) you might encounter some issues. I'd blame google but they are almost never down. :p

 Respond  
L0ckp1ck   -  Feb 16, 2010

this does not work. it lags the shit out of mIRC and sometimes when you type it doesn't show up at all

 Respond  
GuitarMasterx7   -  Jan 26, 2010

ha ima try use this to mess wif some german speaking ppl later
idk why but it sounds fun an interesting >.>

i havent tried it yet but its writen pretty neatly

 Respond  
Serpentsounds   -  Jan 24, 2010

Hahaha no, just that I plan to add a bit more soon. So it's a rough draft, so to speak. But not in the sense that it's in dire need of revision, because as far as I've tested it functions. :p

 Respond  
Jethro   -  Jan 24, 2010

Nice work, but > This is a (working) rough draftare you insinuating there'll be bugs or something? lol

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.