[SCRIPT] Translator

Intended for use on live public servers.
8 posts Page 1 of 1 First unread post
thepolm3
Scripter
Scripter
Posts: 424
Joined: Sat Feb 16, 2013 10:49 pm


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
Attachments
translate.py
(3 KiB) Downloaded 180 times
Last edited by thepolm3 on Tue Jul 02, 2013 7:00 pm, edited 1 time in total.
Venator
League Participant
League Participant
Posts: 1225
Joined: Wed Nov 07, 2012 8:32 pm


Getting all my internetmonies if this makes brs n polaks unable to spam in polish or portugese.
thepolm3
Scripter
Scripter
Posts: 424
Joined: Sat Feb 16, 2013 10:49 pm


yer what ? :)
Test.delta
Deuce
Posts: 6
Joined: Sat Jun 29, 2013 11:30 am


Polm3, would you be willing to fix this script's structure as I cannot seem to implement it into a server build?
thepolm3
Scripter
Scripter
Posts: 424
Joined: Sat Feb 16, 2013 10:49 pm


in what way?
Test.delta
Deuce
Posts: 6
Joined: Sat Jun 29, 2013 11:30 am


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.
Test.delta
Deuce
Posts: 6
Joined: Sat Jun 29, 2013 11:30 am


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!
thepolm3
Scripter
Scripter
Posts: 424
Joined: Sat Feb 16, 2013 10:49 pm


well I fixed it in the global script as well
:)
hope you enjoy!
8 posts Page 1 of 1 First unread post
Return to “Completed Releases”

Who is online

Users browsing this forum: No registered users and 21 guests