def translate(self, text):
		"""
		lang = LanguageDetector().detect(text)
		lang = "en"
		if lang != None:
			translated = Translator().translate(text, "es", lang)
			#lines = translated.rsplit(os.linesep)
			lines = translated.rsplit("\r")
		else:
			"CANNOT Translate"	
		"""
		try:
			#import translate2
			#translated = translate2.translate("en", "sk", text[:1000]).rsplit("|")
			print "TRANSLATE %s" % languages[self.language]
			#print text.decode('utf8', 'ignore')
			#print len(text)
			#print text
			translated = Translator().translate(text.encode('utf8', 'ignore'), languages[self.language])
			if translated != None:
				"""
				try:
					import chardet
					encoding = chardet.detect(translated)
					print encoding
				except: pass
				"""
				return translated.encode('utf8', 'ignore')
			return ""
		except Exception, e:
			print e
			traceback.print_exc()
			return ""
def g_trans(lang, data):

    try:
        translate = Translator().translate
        #str = "mi nombre es gonsalves anthony"
        #decoded_string = str.encode('utf8', 'ignore').strip().replace("\'", r"\'")
        trans = translate(data, str(lang), 'en')
        #file = open('translated_text.txt', 'a')
        #pretty = pprint.pprint(trans)
        #file.write(pretty)
        pprint(trans.decode('string_escape').decode('utf8'))
        #file.close()

    except TranslationError, e:
        pprint(e) 
Beispiel #3
0
class language(pipe):
	def __init__(self, parameters={}, child=None):
		pipe.__init__(self,parameters,child)
		self.pipe = "language"
		self.languages = parameters['languages'] # array of languages
		self.child = child
		self.translator = Translator()

	def translate(self,string,toLang):
		return self.translator.translate(string, lang_to=toLang)

	def transform(self, string):
		transformed = string
		for i in (range(0,len(self.languages))):
			#print "from "+self.langs[i]+" to "+self.langs[i+1]
			transformed = self.translate(transformed,self.languages[i])
		return transformed
Beispiel #4
0
	def __init__(self, parameters={}, child=None):
		pipe.__init__(self,parameters,child)
		self.pipe = "language"
		self.languages = parameters['languages'] # array of languages
		self.child = child
		self.translator = Translator()
"""
translate.py
Loop through the lines of a text file downloaded from the Internet.
Convert each line from a sequence of bytes to a string of characters and translate them.
need to run pip install googletrans on BMCC Mac, or py -3 -m pip install googletrans
"""

# Prob doesnt work yet, couldnt test it at work because of firewalls
import sys
import urllib.request
from xgoogle.translate import Translator

translator = Translator()

language_code = """
Language Code
-------- ----
Afrikaans       af
Albanian        sq
Arabic  ar
Belarusian      be
Bulgarian       bg
Catalan         ca
Chinese Simplified      zh-CN
Chinese Traditional     zh-TW
Croatian        hr
Czech   cs
Danish  da
Dutch   nl
English         en
Estonian        et