Hi,

your problem has nothing to do with wtforms (you would have the same
e.g. with Deform. It only has to do with the moment at your code is read
and executed by Python.

I guess your module is executed during Pyramid's startup phase, before
any request is even made. A that time, "translating the messages into
the request's locale" has no meaning whatsoever. Unless you want to
translate the strings to your site's default locale?

You should probable define your class inside a view, i.e. :

@view_config(...)
def my_View(request):

  _ = request.translate

  class SignupForm(Form):
    ...


Hope this helps,

Laurent.


Le 10/06/13 17:34, unknown...@gmail.com a écrit :
> Hello!
> 
> I'm using i18n exactly as described
> in 
> http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/templates/mako_i18n.html,
> which works fine for views and templates (mako). But I can't get i18n to
> work in my forms (wtforms).
> 
> forms.py:
> ---
> from wtforms import Form, ...
> from myproject.subscribers import tsf as _   # ???
> #from pyramid.i18n import TranslationString as _
> 
> class SignupForm(Form):
>     firstname = TextField(_(u'First name'),
> [validators.Required(message=_(u'This field is required.'))])
>     lastname = TextField(_(u'Last name'),
> [validators.Required(message=_(u'This field is required.'))])
>     email = EmailField(_(u'Email address'),
> [validators.Required(message=_(u'This field is required.')),
> validators.Email(message=_(u'Invalid email address.'))])
> 
> The strings do appear in my locale/.po files, but they are not being
> used when rendering the form.
> I understand it has something to do with 'request', but don't know how
> to set _ = request.translate (as I do it in my views).
> 
> 
> I've also
> followed 
> http://wtforms.simplecodes.com/docs/1.0.4/i18n.html#translating-built-in-messages
> and made my custom translations object.
> 
> forms.py:
> ---
> ...
> from pyramid.threadlocal import get_current_request
> 
> class Translator(object):
>     def __init__(self, request):
>         self.translate = request.translate
>         self.localizer = request.localizer
> 
>     def gettext(self, string):
>         return self.translate(string)
> 
>     def ngettext(self, singular, plural, n):
>         return self.localizer.pluralize(singular, plural, n)
> 
> class ExtendedForm(Form):
>     def _get_translations(self):
>         request = get_current_request()
>         return Translator(request)
> 
> class SignupForm(ExtendedForm):
>     firstname = TextField(_(u'First name'), [validators.Required()])
> 
> But this only translates build-in strings ("This field is required.")
> and not the labels ("First name").
> 
> 
> How/What do I have to import to make i18n work with my forms?
> 
> -- 
> 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.
>  
>  


-- 
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