DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PyS60 - BabelFish

11.28.2006
| 7158 views |
  • submit to reddit
        // Translate from language A to language B
// code not complete but it works

import urllib

####################################################################################### <BabelFish>
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('<div style=padding:10px;>') + 25
            stop = responde.find('</div>', start)
        
            return responde[start:stop]
        
        except Exception, error:
            return '-' + str(error)
####################################################################################### </BabelFish>

####################################################################################### <BabelFishUI>
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<Z-TEAM@Libero.it>', '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')
####################################################################################### </BabelFishUI>

if __name__ == '__main__':
    
    BabelFishUI()