Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #541 (closed defect: fixed)

Opened 13 years ago

Last modified 12 years ago

[PATCH] Fix for google_translator()

Reported by: bjourne@… Owned by: anonymous
Priority: normal Milestone: 0.9
Component: Toolbox Version:
Severity: normal Keywords:
Cc:

Description

Using the I'm feeling lucky function in admi18n gave me the following tb:

Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py", line 99, in _run
    self.main()
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/_cphttptools.py", line 247, in main
    body = page_handler(*virtual_path, **self.params)
  File "<string>", line 3, in po_view
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r713-py2.4.egg/turbogears/controllers.py", line 189, in expose
    func, tg_format, html, fragment, *args, **kw)
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r713-py2.4.egg/turbogears/database.py", line 193, in run_with_transaction
    retval = func(*args, **kw)
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r713-py2.4.egg/turbogears/controllers.py", line 201, in _execute_func
    output = errorhandling.try_call(func, self, *args, **kw)
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r713-py2.4.egg/turbogears/errorhandling.py", line 58, in try_call
    output = func(self, *args, **kw)
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r713-py2.4.egg/turbogears/toolbox/admi18n/__init__.py", line 122, in po_view
    self.google_translate(code,from_lang,to_lang,kargs)
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r713-py2.4.egg/turbogears/toolbox/admi18n/__init__.py", line 105, in google_translate
    translated = turbogears.i18n.utils.google_translate(from_lang,to_lang,msg_id)
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r713-py2.4.egg/turbogears/i18n/utils.py", line 24, in google_translate
    data = match.groups()[0]
AttributeError: 'NoneType' object has no attribute 'groups'

Because google inserted a newline inside the result textarea so the regexp didn't match. I have made a patch which fixed it.

Attachments

tg-fix-google_translate.patch Download (766 bytes) - added by bjourne@… 13 years ago.
Fixes bug in google_translate()

Change History

Changed 13 years ago by bjourne@…

Fixes bug in google_translate()

comment:1 Changed 13 years ago by michele

  • Summary changed from Fix for google_translator() to [PATCH] Fix for google_translator()

comment:2 Changed 13 years ago by alberto

I can't seem to reproduce the problem you're talking about... I've tried translating from english -> spanish, maybe it's language related, can you please describe more concisely how you got at this? (language, etc...)

Thanks

comment:3 Changed 13 years ago by bjourne@…

Here's a testing session:

>>> from turbogears.i18n.utils import google_translate
>>> google_translate("en", "es", "How are you doing?")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r708-py2.4.egg/turbogears/i18n/utils.py", line 24, in google_translate
    data = match.groups()[0]
AttributeError: 'NoneType' object has no attribute 'groups
>>> google_translate("en", "es", "no")
u'no'
>>> google_translate("en", "fr", "no")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r708-py2.4.egg/turbogears/i18n/utils.py", line 24, in google_translate
    data = match.groups()[0]
AttributeError: 'NoneType' object has no attribute 'groups'
>>> google_translate("en", "es", "no")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/TurboGears-0.9a0dev_r708-py2.4.egg/turbogears/i18n/utils.py", line 24, in google_translate
    data = match.groups()[0]
AttributeError: 'NoneType' object has no attribute 'groups'

It seems like google's insertion of the newline character is almost random.

comment:4 Changed 13 years ago by alberto

  • Status changed from new to closed
  • Resolution set to fixed

I see, tested for 100 requests at random intervals and works. commiting in r731 . Thanks!

Note: See TracTickets for help on using tickets.