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

  • SEARCH
  • Home
  • 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 6755493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:24:29+00:00 2026-05-26T13:24:29+00:00

I use i18n for localization like django does using a custom request handler for

  • 0

I use i18n for localization like django does using a custom request handler for switching between different languages and upgrading to python 2.7 this presents trouble since it uses the cookie from django.utils.translation. In worst case I must to move from our custom request handler to some other system that can switch languages for us. Do you have any idea how to resolve this error? Is the django-language cookie still available like before? What are my other alternatives for localization that don’t depend on django? Babel? Could I still use my old .po and .mo files? Should I use some other Cookie class? My custom request handler class:

from django.utils import translation

class I18NHandler(webapp.RequestHandler):   

    def initialize(self, request, response):
        webapp.RequestHandler.initialize(self, request, response)
        self.request.COOKIES = Cookies(self)
        self.request.META = os.environ
        self.reset_language()

    def reset_language(self):

        # Decide the language from Cookies/Headers

        language = translation.get_language_from_request(self.request)
        translation.activate(language)
        self.request.LANGUAGE_CODE = translation.get_language()

        # Set headers in response

        self.response.headers['Content-Language'] = \
            translation.get_language()


class Cookies(UserDict.DictMixin):

    def __init__(self, handler, **policy):
        self.response = handler.response
        self._in = handler.request.cookies
        self.policy = policy
        if 'secure' not in policy \
            and handler.request.environ.get('HTTPS', '').lower() \
            in ['on', 'true']:
            policy['secure'] = True
        self._out = {}

    def __getitem__(self, key):
        if key in self._out:
            return self._out[key]
        if key in self._in:
            return self._in[key]
        raise KeyError(key)

    def __setitem__(self, key, item):
        self._out[key] = item
        self.set_cookie(key, item, **self.policy)

    def __contains__(self, key):
        return key in self._in or key in self._out

    def keys(self):
        return self._in.keys() + self._out.keys()

    def __delitem__(self, key):
        if key in self._out:
            del self._out[key]
            self.unset_cookie(key)
        if key in self._in:
            del self._in[key]
            p = {}
            if 'path' in self.policy:
                p['path'] = self.policy['path']
            if 'domain' in self.policy:
                p['domain'] = self.policy['domain']
            self.delete_cookie(key, **p)

    # begin WebOb functions

    def set_cookie(
        self,
        key,
        value='',
        max_age=None,
        path='/',
        domain=None,
        secure=None,
        httponly=False,
        version=None,
        comment=None,
        ):
        """
        Set (add) a cookie for the response
        """

        cookies = BaseCookie()
        cookies[key] = value
        for (var_name, var_value) in [
            ('max-age', max_age),
            ('path', path),
            ('domain', domain),
            ('secure', secure),
            ('HttpOnly', httponly),
            ('version', version),
            ('comment', comment),
            ]:
            if var_value is not None and var_value is not False:
                cookies[key][var_name] = str(var_value)
            if max_age is not None:
                cookies[key]['expires'] = max_age
        header_value = cookies[key].output(header='').lstrip()
        self.response.headers._headers.append(('Set-Cookie',
                header_value))

    def delete_cookie(
        self,
        key,
        path='/',
        domain=None,
        ):
        """
        Delete a cookie from the client.  Note that path and domain must match
        how the cookie was originally set.
        This sets the cookie to the empty string, and max_age=0 so
        that it should expire immediately.
        """

        self.set_cookie(key, '', path=path, domain=domain, max_age=0)

    def unset_cookie(self, key):
        """
        Unset a cookie with the given name (remove it from the
        response).  If there are multiple cookies (e.g., two cookies
        with the same name and different paths or domains), all such
        cookies will be deleted.
        """

        existing = self.response.headers.get_all('Set-Cookie')
        if not existing:
            raise KeyError('No cookies at all have been set')
        del self.response.headers['Set-Cookie']
        found = False
        for header in existing:
            cookies = BaseCookie()
            cookies.load(header)
            if key in cookies:
                found = True
                del cookies[key]
            header = cookies.output(header='').lstrip()
            if header:
                self.response.headers.add('Set-Cookie', header)
        if not found:
            raise KeyError('No cookie has been set with the name %r'
                           % key)

Error
Trace:

Traceback (most recent call last):
  File "/base/data/home/apps/s~montaoproject/main.354356023835363013/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~montaoproject/main.354356023835363013/i18n.py", line 781, in get
    cookie_django_language
  File "/base/data/home/apps/s~montaoproject/main.354356023835363013/util.py", line 65, in __setitem__
    self.set_cookie(key, item, **self.policy)
  File "/base/data/home/apps/s~montaoproject/main.354356023835363013/util.py", line 120, in set_cookie
    self.response.headers._headers.append(('Set-Cookie',
AttributeError: ResponseHeaders instance has no attribute '_headers'

Thanks in advance for any suggestion.

  • 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-05-26T13:24:29+00:00Added an answer on May 26, 2026 at 1:24 pm

    I believe this is because WebOb has been upgraded with the new Python 2.7 runtime.

    You should use:

    res.headers.add('Set-Cookie', header_value)
    

    As pointed in the webob documentation

    Note that webapp has also been replaced by webapp2.

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

Sidebar

Related Questions

I'd like to use a database to store i18n key/value pairs so we can
I would like to use I18n.t call in an initializer file. Unfortunately, it doesn't
I want use i18n internationalization. CRUD its works fine, but with custom routes something
I would like to know if there is a way to use i18n with
When people discuss about internationalization, they use the word i18n more often. For first
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
I am writing a Django app for use in a country where they use
Goal: I want to use jsf`s i18n Scenario: creating resource bundle (utf-8) file info:
How can i use global i18n translations for all applications in a symfony project?

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.