Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

PyS60 - BabelFish

DZone's Guide to

PyS60 - BabelFish

·
Free Resource
// Translate from language A to language B
// code not complete but it works


import urllib

####################################################################################### 
  
   
class BabelFish(object):
    
    def translate(self, lang, message):
        
        try:
            url = urllib.URLopener()
        
            query = urllib.urlencode({
                                      'doit':'done',
                                      'intl':'1',
                                      'lp':lang,
                                      'tt':'urltext',
                                      'urltext':message
                                      })
        
            responde = url.open('http://babelfish.altavista.com/tr', query).read()
        
            start = responde.find('
   
') + 25 stop = responde.find('
', start) return responde[start:stop] except Exception, error: return '-' + str(error) #######################################################################################
####################################################################################### from graphics import * import appuifw import e32 class BabelFishUI(object): def __init__(self): self.__lock = e32.Ao_lock() self.__img = Image.new((176, 144)) self.__language = 'it_en' self.__textUI = None appuifw.app.exit_key_handler = lambda:self.__lock.signal() appuifw.app.title = u'BabelFish v1.0' appuifw.app.body = self.__canvas = appuifw.Canvas(redraw_callback=self.updateScreen) appuifw.app.menu = [(u'Translate', lambda:self.__translateUI()), (u'About', lambda:appuifw.note(u'BabelFish: v1.0", "Created by\nWhite Tiger\n ', 'info')), (u'Exit', lambda:self.__lock.signal)] self.updateScreen(None) self.__menuMain = appuifw.app.menu self.__bgMain = appuifw.app.body self.__lock.wait() def updateScreen(self, rect): self.__canvas.blit(self.__img) def __back(self): appuifw.app.menu = self.__menuMain appuifw.app.body = self.__bgMain appuifw.app.set_tabs([u'Back'], lambda x:None) def __translateUI(self): self.__textUI = appuifw.Text() appuifw.app.menu = [(u'Translate', lambda:self.__translate()), (u'Language', lambda:self.__setLanguage()), (u'Clear', lambda:self.__textUI.clear()), (u'Back', lambda:self.__back())] appuifw.app.body = self.__textUI def __setLanguage(self): resp = appuifw.selection_list([u'italiano-inglese', u'inglese-italiano', u'inglese-francese', u'francese-inglese', u'inglese-tedesco', u'tedesco-inglese', u'francese-italiano', u'italiano-francese'], 1) if resp == 0: self.__language = 'it_en' elif resp == 1: self.__language = 'en_it' elif resp == 2: self.__language = 'en_fr' elif resp == 3: self.__language = 'fr_en' elif resp == 4: self.__language = 'en_de' elif resp == 5: self.__language = 'de_en' elif resp == 6: self.__language = 'fr_it' elif resp == 7: self.__language = 'it_fr' def __translate(self): babel = BabelFish() resp = babel.translate(self.__language, self.__textUI.get()) if resp[0] == '-': self.__textUI.set(unicode(resp[1:])) else: self.__textUI.set(unicode(': ' +self.__textUI.get() + '\n: ' + resp)) appuifw.note(u'Translate', 'conf') ####################################################################################### if __name__ == '__main__': BabelFishUI()
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}