Created
March 4, 2010 03:07
-
-
Save pklaus/321358 to your computer and use it in GitHub Desktop.
Babelfish Translator for Symbian Phones (using PyS60).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: UTF8 -*- | |
# originally from whitetiger on <http://snippets.dzone.com/posts/show/3047> | |
# modified 2010 by Philipp Klaus <philipp.l.klaus AT web-dot-de> | |
import urllib | |
####################################################################################### <BabelFish> | |
class BabelFish(object): | |
def translate(self, lang, message): | |
try: | |
url = FirefoxOpener() | |
query = urllib.urlencode({ | |
'doit':'done', | |
'intl':'1', | |
'lp':lang, | |
'tt':'urltext', | |
'urltext':message | |
}) | |
response = url.open('http://babelfish.yahoo.com/translate_txt', query).read() | |
result_markup_begin = u'<div id="result"><div style="padding:0.6em;">' | |
result_markup_end = u'</div>' | |
start = response.find(result_markup_begin) + len(result_markup_begin) | |
stop = response.find(result_markup_end, start) | |
return response[start:stop] | |
except Exception, error: | |
return '-' + str(error) | |
####################################################################################### </BabelFish> | |
####################################################################################### <FirefoxOpener> | |
class FirefoxOpener(urllib.FancyURLopener): | |
version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11' | |
####################################################################################### </FirefoxOpener> | |
####################################################################################### <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<[email protected]>', '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'Italian - English', u'English - Italian', u'English - French', u'French - English', u'English - German', u'German - English', | |
u'French - Italian', u'Italian - French'], 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obexftp.exe -b 00:1d:5d:5b:53:fc -B 11 --chdir /E:/Python/ --put ./babelfish.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/bin/sh | |
obexftp -b 00:1d:5d:5b:53:fc -B 11 --chdir /E:/Python/ --put ./babelfish.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment