diff --git a/modules/googletranslate/backend.py b/modules/googletranslate/backend.py
index 50f56703..c7880fd8 100644
--- a/modules/googletranslate/backend.py
+++ b/modules/googletranslate/backend.py
@@ -18,21 +18,32 @@
# along with weboob. If not, see .
"backend for http://translate.google.com"
-from weboob.capabilities.translate import ICapTranslate
-from weboob.tools.backend import BaseBackend, BackendConfig
+
+from weboob.capabilities.translate import ICapTranslate, Translation, TranslationFail
+from weboob.tools.backend import BaseBackend
+
from .browser import GoogleTranslateBrowser
+
__all__ = ['GoogleTranslateBackend']
+
class GoogleTranslateBackend(BaseBackend, ICapTranslate):
MAINTAINER = 'Lucien Loiseau'
EMAIL = 'loiseau.lucien@gmail.com'
VERSION = '0.c'
LICENSE = 'AGPLv3+'
- STORAGE = {'seen': {}}
NAME = 'googletranslate'
DESCRIPTION = u'Google translation web service'
BROWSER = GoogleTranslateBrowser
def translate(self, lan_from, lan_to, text):
- return self.browser.translate(lan_from, lan_to, text)
+ translation = Translation(0)
+ translation.lang_src = lan_from
+ translation.lang_dst = lan_to
+ translation.text = self.browser.translate(lan_from, lan_to, text)
+
+ if translation.text is None:
+ raise TranslationFail()
+
+ return translation
diff --git a/modules/googletranslate/browser.py b/modules/googletranslate/browser.py
index eb768f7b..dce314ce 100644
--- a/modules/googletranslate/browser.py
+++ b/modules/googletranslate/browser.py
@@ -18,16 +18,16 @@
# along with weboob. If not, see .
-from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound, BrowserIncorrectPassword
-from weboob.tools.browser.decorators import id2url, check_url
-from weboob.capabilities.translate import TranslationFail
+import urllib
+
+from weboob.tools.browser import BaseBrowser
from .pages import TranslatePage
-import urllib
__all__ = ['GoogleTranslateBrowser']
+
class GoogleTranslateBrowser(BaseBrowser):
DOMAIN = 'translate.google.com'
ENCODING = 'UTF-8'
@@ -43,22 +43,17 @@ class GoogleTranslateBrowser(BaseBrowser):
"""
translate 'text' from 'source' language to 'to' language
"""
- try:
- d = {
- 'sl': source,
- 'tl': to,
- 'js': 'n',
- 'prev': '_t',
- 'hl': 'en',
- 'ie': 'UTF-8',
- 'layout': '2',
- 'eotf': '1',
- 'text': text,
- }
- self.location('http://'+self.DOMAIN, urllib.urlencode(d))
- translation = self.page.get_translation()
- return translation
-
- except TranslationFail:
- return "no translation available"
-
+ d = {
+ 'sl': source.encode('utf-8'),
+ 'tl': to.encode('utf-8'),
+ 'js': 'n',
+ 'prev': '_t',
+ 'hl': 'en',
+ 'ie': 'UTF-8',
+ 'layout': '2',
+ 'eotf': '1',
+ 'text': text.encode('utf-8'),
+ }
+ self.location('http://'+self.DOMAIN, urllib.urlencode(d))
+ translation = self.page.get_translation()
+ return translation
diff --git a/modules/googletranslate/pages.py b/modules/googletranslate/pages.py
index 4710fed7..ec44b4a1 100644
--- a/modules/googletranslate/pages.py
+++ b/modules/googletranslate/pages.py
@@ -17,15 +17,17 @@
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see .
-from datetime import date
+
from weboob.tools.browser import BasePage
+__all__ = ['TranslatePage']
+
+
class TranslatePage(BasePage):
def get_translation(self):
- for span in self.document.getiterator('span'):
- if (span.attrib.get('id', '') == 'result_box'):
- for children in span.getchildren():
- return children.text
-
+ boxes = self.parser.select(self.document.getroot(), 'span#result_box', 1).findall('span')
+ if len(boxes) == 0:
+ return None
+ return u'\n'.join([unicode(box.text) for box in boxes])
diff --git a/weboob/applications/translaboob/translaboob.py b/weboob/applications/translaboob/translaboob.py
index 93608a56..037f8e8b 100644
--- a/weboob/applications/translaboob/translaboob.py
+++ b/weboob/applications/translaboob/translaboob.py
@@ -18,54 +18,61 @@
# along with weboob. If not, see .
-import sys
-
-import os
-import sys
-import codecs
-import locale
-
from weboob.capabilities.translate import ICapTranslate
from weboob.tools.application.repl import ReplApplication
+from weboob.tools.application.formatters.iformatter import IFormatter
+
__all__ = ['Translaboob']
+
+class TranslationFormatter(IFormatter):
+ MANDATORY_FIELDS = ('id', 'text')
+
+ def flush(self):
+ pass
+
+ def format_dict(self, item):
+ backend = item['id'].split('@', 1)[1]
+ result = u'%s* %s%s\n\t%s' % (self.BOLD, backend, self.NC, item['text'].replace('\n', '\n\t'))
+ return result
+
+class XmlTranslationFormatter(IFormatter):
+ MANDATORY_FIELDS = ('id', 'lang_src', 'lang_dst', 'text')
+
+ def flush(self):
+ pass
+
+ def format_dict(self, item):
+ backend = item['id'].split('@', 1)[1]
+ return u'\n%s\n' % (backend, item['text'])
+
class Translaboob(ReplApplication):
APPNAME = 'translaboob'
VERSION = '0.c'
COPYRIGHT = 'Copyright(C) 2012 Lucien Loiseau'
DESCRIPTION = 'Console application to translate text from one language to another'
CAPS = ICapTranslate
-
- def main(self, argv):
- return ReplApplication.main(self, argv)
+ EXTRA_FORMATTERS = {'translation': TranslationFormatter,
+ 'xmltrans': XmlTranslationFormatter,
+ }
+ COMMANDS_FORMATTERS = {'translate': 'translation',
+ }
def do_translate(self, line):
- lan_from, lan_to, text = self.parse_command_args(line, 3, 1)
"""
- translate
- translate from one language to another,
- : source language
- : destination language
- : language to translate, standart input if - is given
+ translate FROM TO [TEXT]
+
+ Translate from one language to another.
+ * FROM : source language
+ * TO : destination language
+ * TEXT : language to translate, standart input if - is given
"""
+
+ lan_from, lan_to, text = self.parse_command_args(line, 3, 2)
+
if not text or text == '-':
text = self.acquire_input()
- print "from : "+lan_from+" to : "+lan_to
- print ""
- print text
- print ""
-
for backend, translation in self.do('translate', lan_from, lan_to, text):
- print ""
- print translation
- print ""
-
-
-
-
-
-
-
-
+ self.format(translation)
diff --git a/weboob/capabilities/translate.py b/weboob/capabilities/translate.py
index 753b293d..27a3494f 100644
--- a/weboob/capabilities/translate.py
+++ b/weboob/capabilities/translate.py
@@ -18,10 +18,7 @@
# along with weboob. If not, see .
-from datetime import date, datetime
-
-from .base import CapBaseObject, Field, StringField, DateField, DecimalField, IntField
-from .collection import ICapCollection
+from .base import IBaseCap, CapBaseObject, StringField
__all__ = ['TranslationFail', 'ICapTranslate']
@@ -36,21 +33,27 @@ class TranslationFail(Exception):
Exception.__init__(self, msg)
-class ICapTranslate(ICapCollection):
+class Translation(CapBaseObject):
+ """
+ Translation.
+ """
+ lang_src = StringField('Source language')
+ lang_dst = StringField('Destination language')
+ text = StringField('Translation')
+
+
+class ICapTranslate(IBaseCap):
"""
Capability of online translation website to translate word or sentence
"""
+
def translate(self, source_language, destination_language, request):
"""
- perfom a translation
-
+ Perfom a translation.
+
:param source_language: language in which the request is written
:param destination_language: language to translate the request into
:param request: the sentence to be translated
+ :rtype: Translation
"""
- raise TranslationFail()
-
-
-
-
-
+ raise NotImplementedError()