Hello!
First, thank you very much for your response!
I do understand my problem is not wtforms-specific, but I didn't find and
info
on how to solve this issue when using deform, formencode or any other
library...
The solution you provided really does work, however... :/
I really wouldn't like to write my forms inside views (there are alot of
forms and
validation and I would like to separate those; views in views.py and forms
in forms.py).
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)
...
But then I dont know how to use _, or even if thats possible...
On Wednesday, June 12, 2013 8:55:31 AM UTC+2, Eldav wrote:
>
> 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, [email protected] <javascript:> 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 [email protected] <javascript:>.
> > To post to this group, send email to
> > [email protected]<javascript:>.
>
> > 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.