Page 1 of 1

[SCRIPT] Translator

Posted: Fri Apr 12, 2013 9:04 am
by thepolm3
Hey all!
Here is my newest python code thing; Translator!
what it is is you do /tr en (english) or /tr de (german) and it translates everyone's messages into that language!
There is also a server language that you can choose to force, translating everyone's ramblings into your chosen language.
Without forcing though, it WILL still translate player's speech into the command prompt

list of available languages and their two letter codes:

THE TRANSLATOR IS NOT MINE all credit to Mouuff

All I did was incorporate it into pyspades.
Here's the code:
Code: Select all

"""
Translates a players words for all players to understand.

BASED ON THIS https://github.com/mouuff/Google-Translate-API/blob/master/gtranslate.py
all translating done by google translate, all credit to Google

edited for pyspades  by thepolm3
"""

import urllib2
from commands import add,name,get_player

server_lang="en"
force_lang=False

#NOT by thepolm3, script by mouuff; https://github.com/mouuff/Google-Translate-API/blob/master/gtranslate.py
def translate(to_translate, to_langage="auto", langage="auto"):
	agents = {'User-Agent':"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)"}
	before_trans = 'class="t0">'
	link = "http://translate.google.com/m?hl=%s&sl=%s&q=%s" % (to_langage, langage, to_translate.replace(" ", "+"))
	request = urllib2.Request(link, headers=agents)
	page = urllib2.urlopen(request).read()
	result = page[page.find(before_trans)+len(before_trans):]
	result = result.split("<")[0]
	return result


def tr(connection,language=None):
        lps=connection.protocol.langplayers
        if connection.name in lps and not language:
                del(lps[lps.index(connection.name)])
                connection.send_chat("Translating is now off")
        else:
                connection.send_chat(translate("Translating is now on.",language).decode('utf-8'))
                connection.lang=language
                connection.protocol.langplayers.append(connection.name)

add(tr)
def apply_script(protocol, connection, config):
        
	class TranslateConnection(connection):
                lang=None
		def on_chat(self, value, global_message):
                        if force_lang:
                                value=translate(value,server_lang).decode("utf-8")
                        self.protocol.translateForAllPlayers(value,self.name,global_message)
                        return connection.on_chat(self, value, global_message)
        
	class TranslateProtocol(protocol):
                langplayers=[]
                def translateForAllPlayers(self,words,chattername,glob):
                        chatter=get_player(self,chattername)
                        for name in self.langplayers:
                                player=get_player(self,name)
                                trans=translate(words,player.lang).decode('utf-8')
                                if words.lower()!=trans.lower():
                                        if glob:
                                                player.send_chat(chattername+" ("+chatter.team.name+"): "+trans)
                                        elif player.team.name==chatter.team.name:
                                                player.send_chat(chattername+": "+words)
                        if words.lower()!=translate(words,server_lang).decode('utf-8').lower():
                                print("<"+chattername+"> ("+translate(words,server_lang).decode('utf-8')+")")
                                                                
        
	return TranslateProtocol, TranslateConnection

Enjoy! I'm off for a sleep Blue_Sleeping

Re: [SCRIPT] Translator

Posted: Wed Apr 17, 2013 8:42 pm
by Venator
Getting all my internetmonies if this makes brs n polaks unable to spam in polish or portugese.

Re: [SCRIPT] Translator

Posted: Wed Apr 24, 2013 3:59 pm
by thepolm3
yer what ? :)

Re: [SCRIPT] Translator

Posted: Sat Jun 29, 2013 11:34 am
by Test.delta
Polm3, would you be willing to fix this script's structure as I cannot seem to implement it into a server build?

Re: [SCRIPT] Translator

Posted: Sat Jun 29, 2013 11:59 am
by thepolm3
in what way?

Re: [SCRIPT] Translator

Posted: Sat Jun 29, 2013 9:45 pm
by Test.delta
Well, whenever I try to operate the PySnip build with the translate.py script, the console announces:

File "C:\Users\Test\Desktop\dist(official)\scritps\translate.py, line 56 in translateForAlllPlayers
NameError: global name trans is not defined.

In other words, whenever I turn translation on, messages that I send such as Sayonara are not translated but omitted from the global chat as well as even English words.

Re: [SCRIPT] Translator

Posted: Sat Jun 29, 2013 10:49 pm
by Test.delta
Oh sorry, never mind about fixing the script. My friend apparently was able to fix it for me. :) Thank you anyways for listening to my request!

Re: [SCRIPT] Translator

Posted: Sun Jun 30, 2013 8:43 am
by thepolm3
well I fixed it in the global script as well
:)
hope you enjoy!