> Right now I'm trying to send request.translate to SignupForm via:
> 
> @view_config(...)
> def signup(request):
>     customer = Customer()
>     form = SignupForm(request.POST, customer, _=request.translate)
>     ...

I guess this can't work either, for the same reason. But this gave me an
idea :-)

Basically, the problem is that the form definition is evaluated at
initialization time, but the translation proper must be done only during
each request.

So, I tried this, and it works for me (Deform + Mako): I created a
"_lazy" class, which is a replacement for "_" :


from pyramid.threadlocal import get_current_request

class _lazy(object):

    def __init__(self, msg):
        self.msg = msg

    def __str__(self):
        request = get_current_request()
        return request.translate(self.msg)


(Yes, I know, using "get_current_request" should be avoided as much as
possible ;-)
http://docs.pylonsproject.org/projects/pyramid/en/latest/api/threadlocal.html)


So, when my form is defined, each message is instantiated as a "_lazy"
object:


class SignupForm(Form):
    firstname = TextField(_lazy(u'First name'),
[validators.Required(message=_lazy(u'This field is required.'))])
    lastname = TextField(_lazy(u'Last name'),
[validators.Required(message=_lazy(u'This field is required.'))])


Then, I'm supposing that when the templating system renders the
TextFields into HTML, it will convert them to strings/unicode/...

That's why I do the actual translation in the __str__ method, using the
get_current_request() request.


Hope this helps,


Laurent.



-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to