Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:23:34+00:00 2026-06-03T18:23:34+00:00

Pyramid documentation shows us how to use i18n inside views (and templates as well

  • 0

Pyramid documentation shows us how to use i18n inside views (and templates as well). But how to does one use it outside of views and templates where we have no access to current request (for example, in forms and models)?

@Michael said to pass request to models and forms. But is it right? I mean if form fields defines before __init__() method calls, the same with models. They don’t see any parameters from views…

In Pylons we could simply use get_lang() and set_lang() and define preferable language in parent controller and then use ugettext() and ungettext() in any place we want without calling it from request directly every possible time (in views).

How to do that in Pyramid? Note that the language must be set from user’s settings (session, cookies, db, etc).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:23:41+00:00Added an answer on June 3, 2026 at 6:23 pm

    My solution is to create the form class when it’s needed with localizer as parameter. For example

    forms.py

    class FormFactory(object):
    
        def __init__(self, localizer):
            self.localizer = localizer
            _ = self.localizer
            self.required_msg = _(u'This field is required.')
            self.invalid_email_msg = _(u'Invalid email address.')
            self.password_not_match_msg = _(u'Password must match')
    
        def make_contact_form(self):
            _ = self.localizer
            class ContactForm(Form):
                email = TextField(_(u'Email address'), [
                    validators.Required(self.required_msg),
                    validators.Email(self.invalid_email_msg)
                ])
                content = TextAreaField(_(u'Content'), [
                    validators.Required(self.required_msg)
                ])
            return ContactForm
    

    When you need to use the form

    @view_config(route_name='front_pages.contact_us',
                 renderer='myweb:templates/front_pages/contact_us.genshi')
    def contact_us(request):
        """Display contact us form or send mail
    
        """
        _ = get_localizer(request)
    
        factory = FormFactory(_)
        ContactForm = factory.make_contact_form()
        form = ContactForm(request.params)
        return dict(form=form)
    

    As you can see, we get the localizer in the view, and pass it to the FormFactory, then create a form with that factory. By doing that, all messages in the form was replaced with current locale language.

    Likewise, you can do the same with model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Pyramid app that uses Chameleon for its templates. I would like
Since Pyramid does not have any form dependencies, I need recommendations for form handling.
Does Pyramid i18n of .pt files support HTML5 mark-up? Currently the i18n tag extractor
When I use the usual Python console, the import works well, but when I
I'm new to pyramid as well as the traversal concept. I have a basic
In Pyramid , add_notfound_view(append_slash=True) will cause a request which does not match any view,
I have a typical Pyramid web application setup. The application directory (I don't know
I am using latest Pyramid to build a web app. Somehow we have started
I am new to pyramid. I use python2.7, MSSQL, slqalchemy.mssql and use the gbk
Although I have set pyramid.reload_templates to true e.g. pyramid.reload_templates = true , each time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.