Simpleform Localization in Pyramid

In a recent project I had to localize the errors thrown by the pyramid_simpleform package. Googling for information I couldn’t find how to do it, so here’s what worked for me at the end.

  from pyramid.i18n import get_locale_name
  from pyramid_simpleform import Form,State
  from formencode import api as formencode_api
  def includeme(config):
    config.add_route('login', '/login')
  def login(request):
     set the language in FormEncode according to the request url param _LOCALE_
     form = Form(request,
     set an empty gettext translation function,
     since FormEncode has one already
     configured in the set_stdtranslation function
     form.state._ = ''
     return dict(renderer=FormRenderer(form))

And that’s it, try it for example Make sure the action param in your form passes the _LOCALE_ value if the method is set to post.