[kupfer: 1/13] plugin.google_translate: add first version.
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer: 1/13] plugin.google_translate: add first version.
- Date: Tue, 3 Nov 2009 12:00:02 +0000 (UTC)
commit 9a1192d5e4bb76df77b4704459dd43f075392e1d
Author: Karol BÄ?dkowski <karol bedkowsk+gh gmail com>
Date: Thu Oct 29 13:11:24 2009 +0100
plugin.google_translate: add first version.
kupfer/plugin/google_translate.py | 128 +++++++++++++++++++++++++++++++++++++
1 files changed, 128 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/plugin/google_translate.py b/kupfer/plugin/google_translate.py
new file mode 100644
index 0000000..d7ad2b6
--- /dev/null
+++ b/kupfer/plugin/google_translate.py
@@ -0,0 +1,128 @@
+# -*- coding: UTF-8 -*-
+from kupfer.objects import Source, Action, TextLeaf, Leaf
+from kupfer import utils
+
+__kupfer_name__ = _("Google Translate")
+__kupfer_actions__ = ("Translate", )
+__description__ = _("Translate text in Google Translate")
+__version__ = ""
+__author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
+
+import httplib
+import urllib
+from urlparse import urlparse
+
+_GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_a/t'
+_GOOGLE_TRANS_LANG_URL = 'http://translate.google.com/translate_t'
+_HEADERS = {
+ 'Host':'translate.google.com',
+ 'User-Agent':'Mozilla/5.0',
+ 'Accept':'text/xml,application/xml,application/xhtml+xml,text/html',
+ 'Referer':'http://translate.google.com/translate_t',
+ 'Content-Type':'application/x-www-form-urlencoded'}
+
+def _translate(text, lang):
+ params = {'sl': 'auto', 'tl': lang, 'text': text, 'client': 't'}
+ url = urlparse(_GOOGLE_TRANSLATE_URL)
+ try:
+ conn = httplib.HTTPConnection(url[1])
+ conn.request("POST", url[2], urllib.urlencode(params), _HEADERS)
+ resp = conn.getresponse()
+ except Exception, err:
+ return _("Error connecting to Google Translate")
+
+ header = resp.getheader("Content-Type")
+ charset= header[header.index("charset=")+8:]
+ if resp.status == 200:
+ data = resp.read().decode(charset)
+ if data[0] == "[":
+ data = data.strip('[]').split(',')
+ return data[0].strip('"')
+ else:
+ return data.strip('"')
+ else:
+ return _("Error")
+
+
+def _languages():
+ url = urlparse(_GOOGLE_TRANS_LANG_URL)
+ data = {}
+ try:
+ conn = httplib.HTTPConnection(url[1])
+ conn.request("GET", url[2])
+ resp = conn.getresponse()
+ except Exception, err:
+ return
+
+ if resp.status == 200:
+ result = resp.read()
+ result = result[result.index('select name=tl'):]
+ result = result[result.index("<option"):result.index("</select>")]
+ rows = result.split("</option>")
+ for row in rows:
+ if row:
+ yield (row[row.index('"')+1:row.rindex('"')],
+ row[row.index('>')+1:])
+
+
+class Translate (Action):
+ def __init__(self):
+ Action.__init__(self, _("Translate"))
+
+ def activate(self, leaf, iobj):
+ text = unicode(leaf.object)
+ dest_lang = iobj.object
+ return _TransateQuerySource(text, dest_lang)
+
+ def is_factory(self):
+ return True
+
+ def item_types(self):
+ yield TextLeaf
+
+ def valid_for_item(self, leaf):
+ return len(leaf.object.strip()) > 0
+
+ def get_description(self):
+ return _("Use Google Translate")
+
+ def get_icon_name(self):
+ return "accessories-dictionary"
+
+ def requires_object(self):
+ return True
+
+ def object_types(self):
+ yield _Language
+
+ def object_source(self, for_item=None):
+ return _LangSource()
+
+
+class _TransateQuerySource(Source):
+ def __init__(self, text, lang):
+ Source.__init__(self, name=_("Translate for %s") % text)
+ self._text = text
+ self._lang = lang
+
+ def is_dynamic(self):
+ return True
+
+ def get_items(self):
+ yield TextLeaf(_translate(self._text, self._lang))
+
+
+class _Language(Leaf):
+ pass
+
+
+class _LangSource(Source):
+ def __init__(self):
+ Source.__init__(self, _("Languages"))
+
+ def get_items(self):
+ for key, name in _languages():
+ yield _Language(key, _("Translate to %s") % name)
+
+ def provides(self):
+ yield _Language
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]