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 #485 (closed defect: fixed)

Opened 13 years ago

Last modified 12 years ago

I18N + Widgets problem: get_locale fails and throws an exception

Reported by: Jorge Godoy <jgodoy@…> Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TG Widgets Version:
Severity: major Keywords:
Cc: jgodoy@…

Description

It looks like something related to I18N + Widgets breaks projects.

godoy@jupiter ~/empresa/clientes/latam/site_amostras % ./start-site_amostras.py 
Traceback (most recent call last):
  File "./start-site_amostras.py", line 24, in ?
    from site_amostras.controllers import Root
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/controllers.py", line 29, in ?
    from novos_usuarios import NovosUsuarios
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/novos_usuarios.py", line 15, in ?
    import clientes
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/clientes.py", line 15, in ?
    class Clientes:
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/clientes.py", line 21, in Clientes
    labeltext = _('Nome abreviado'),
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/tg_gettext.py", line 45, in gettext
    return gettext_f(key, locale, domain)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/tg_gettext.py", line 54, in _gettext
    if locale is None:locale = get_locale()
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/utils.py", line 64, in get_locale
    locale = get_locale_f()
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/utils.py", line 77, in _get_locale
    locale = cherrypy.session.get(locale_key)
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/filters/sessionfilter.py", line 413, in __getattr__
    sess = cherrypy.request._session
  File "/usr/lib/python2.4/site-packages/CherryPy-2.2.0beta-py2.4.egg/cherrypy/__init__.py", line 42, in __getattr__
    childobject = getattr(serving, self.__attrname__)
AttributeError: 'thread._local' object has no attribute 'request'
godoy@jupiter ~/empresa/clientes/latam/site_amostras % 

Attachments

i18n.patch Download (755 bytes) - added by alberto@… 13 years ago.
A patch

Change History

comment:1 Changed 13 years ago by Jorge Godoy <jgodoy@…>

Just as a side note:

godoy@jupiter ~/desenvolvimento/python/TurboGears/trunk % LANG= LC_ALL= svn info . 
Path: .
URL: http://www.turbogears.org/svn/turbogears/trunk
Repository UUID: 77541ad4-5f01-0410-9ede-a1b63cd9a898
Revision: 591
Node Kind: directory
Schedule: normal
Last Changed Author: ronald
Last Changed Rev: 591
Last Changed Date: 2006-01-29 18:02:42 -0200 (Sun, 29 Jan 2006)
Properties Last Updated: 2006-01-18 01:15:42 -0200 (Wed, 18 Jan 2006)

godoy@jupiter ~/desenvolvimento/python/TurboGears/trunk % 

comment:2 Changed 13 years ago by Jorge Godoy <jgodoy@…>

The problem continues on:

jupiter:/home/godoy/desenvolvimento/python/TurboGears/trunk # LANG= svn info .
Path: .
URL: http://www.turbogears.org/svn/turbogears/trunk
Repository UUID: 77541ad4-5f01-0410-9ede-a1b63cd9a898
Revision: 596
Node Kind: directory
Schedule: normal
Last Changed Author: kevin
Last Changed Rev: 596
Last Changed Date: 2006-01-30 11:02:23 -0200 (Mon, 30 Jan 2006)
Properties Last Updated: 2006-01-18 01:15:42 -0200 (Wed, 18 Jan 2006)

jupiter:/home/godoy/desenvolvimento/python/TurboGears/trunk # 

comment:3 Changed 13 years ago by Jorge Godoy <jgodoy@…>

Coming back to r485 the error is different, but related to the same files:

Traceback (most recent call last):
  File "./site_amostras-start.py", line 20, in ?
    from site_amostras.controllers import Root
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/controllers.py", line 29, in ?
    from novos_usuarios import NovosUsuarios
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/novos_usuarios.py", line 15, in ?
    import clientes
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/clientes.py", line 15, in ?
    class Clientes:
  File "/home/godoy/empresa/clientes/latam/site_amostras/site_amostras/clientes.py", line 21, in Clientes
    labeltext = _('Nome abreviado'),
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/tg_gettext.py", line 45, in gettext
    return gettext_f(key, locale, domain)
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/tg_gettext.py", line 54, in _gettext
    if locale is None:locale = get_locale()
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/utils.py", line 64, in get_locale
    locale = get_locale_f()
  File "/home/godoy/desenvolvimento/python/TurboGears/trunk/turbogears/i18n/utils.py", line 77, in _get_locale
    locale = cherrypy.session.get(locale_key)
  File "/usr/lib/python2.4/site-packages/CherryPy-2.1.0-py2.4.egg/cherrypy/lib/filter/sessionfilter.py", line 440, in __getattr__
    sess = cherrypy.request._session
NameError: global name 'cherrypy' is not defined

(All my files from this project have 'import cherrypy')

comment:4 Changed 13 years ago by anonymous

How do you happen to use CherryPy? 2.1? You should have been using CherryPy? 2.2.0-beta.

comment:5 Changed 13 years ago by alberto

I'm using 2.2.0-beta and having the same problem

comment:6 Changed 13 years ago by Jorge Godoy <jgodoy@…>

Just to make it clear that it is widgets related: when I remove the TableForm? widget, the code works OK.

comment:7 Changed 13 years ago by Jorge Godoy <jgodoy@…>

To the "anonymous" above, if you take a look you'll see that with svn HEAD I use CP 2.2.0 beta and with SVN R485 I use what it requested by then: CP 2.1.0. So, the problem is not with the required version of CherryPy?.

comment:8 Changed 13 years ago by Jorge Godoy <jgodoy@…>

  • Cc jgodoy@… added

Adding myself to Cc:, so that I know when this bug report changes.

Changed 13 years ago by alberto@…

A patch

comment:9 Changed 13 years ago by alberto@…

I've uploaded a patch that works around the problem. Though the cause is much deeper:

The exception is raised while the widget classes are being CREATED, this means we're outside any request (hence the AttributeError?: 'thread._local' object has no attribute 'request' exception). The patch simply makes _get_locale catch the excpetion and do nothing.

As i've said, this patch only works around the problem, probably i18n should be fixed to revert to a default locale when there's no request available. I'm not very experienced with gettext but I can foresee some trouble (such as threadsafety issues, as widgets are instantiated once. etc..) so this ticket should remain open.

comment:10 Changed 13 years ago by Dan Jacob

You do not call gettext when initalizing widgets ( or any other code outside of a cherrypy request ) for this very reason. If you want to "mark" a string for translation at request time, use lazy_gettext, for example:

TextField?("name", labeltext = lazy_gettext("Name"))

comment:11 Changed 13 years ago by Jorge Godoy <jgodoy@…>

Indeed, Dan. You're right. It solves the problem to me.

comment:12 Changed 13 years ago by Jorge Godoy <jgodoy@…>

As I mentioned on the mailing list, what would be the drawbacks of using "lazy_gettext" (mapped as "_") everywhere? If there are serious problems with that, how could we make "_" use "lazy_gettext" for widgets?

The convention for i18n is using "_" (thanks to gettext), so I believe that it should be transparent for the developer what function is it using behind the scene.

comment:13 Changed 13 years ago by Dan Jacob

I usually have a separate module for forms in my TG projects, forms.py, so at the top of that I have:

from turbogears.i18n import lazy_gettext as _

then _() calls are detected as usual by the gettext tools.

comment:14 Changed 13 years ago by alberto@…

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

Yep, that solved the problem for me too, thanks Dan! I'm closing the ticket then as I think this is solved.

Note: See TracTickets for help on using tickets.