Ticket #3651: set_language_patch.diff

File set_language_patch.diff, 3.0 KB (added by Fraser Nevett <mail@…>, 17 years ago)

Patch for "option 1" (including updates for the documentation)

  • django/views/i18n.py

     
    99    """
    1010    Redirect to a given url while setting the chosen language in the
    1111    session or cookie. The url and the language code need to be
    12     specified in the GET paramters.
     12    specified in the request parameters. As this view may change how
     13    the user will see the rest of the site, it must be accessed using
     14    a POST request. If a POST request is not used, it should fail
     15    gracefully by returning the user to the appropriate place.
    1316    """
    14     lang_code = request.GET['language']
    15     next = request.GET.get('next', None)
     17    next = request.REQUEST.get('next', None)
    1618    if not next:
    1719        next = request.META.get('HTTP_REFERER', None)
    1820    if not next:
    1921        next = '/'
    2022    response = http.HttpResponseRedirect(next)
    21     if check_for_language(lang_code):
    22         if hasattr(request, 'session'):
    23             request.session['django_language'] = lang_code
    24         else:
    25             response.set_cookie('django_language', lang_code)
     23    if request.method == 'POST':
     24        lang_code = request.REQUEST.get('language', None)
     25        if lang_code and check_for_language(lang_code):
     26            if hasattr(request, 'session'):
     27                request.session['django_language'] = lang_code
     28            else:
     29                response.set_cookie('django_language', lang_code)
    2630    return response
    2731
    2832NullSource = """
  • docs/i18n.txt

     
    547547
    548548(Note that this example makes the view available at ``/i18n/setlang/``.)
    549549
    550 The view expects to be called via the ``GET`` method, with a ``language``
    551 parameter set in the query string. If session support is enabled, the view
    552 saves the language choice in the user's session. Otherwise, it saves the
    553 language choice in a ``django_language`` cookie.
     550The view expects to be called via the ``POST`` method, with a ``language``
     551parameter set in the query string or POST data. If session support is enabled,
     552the view saves the language choice in the user's session. Otherwise, it saves
     553the language choice in a ``django_language`` cookie.
    554554
    555555After setting the language choice, Django redirects the user, following this
    556556algorithm:
    557557
    558     * Django looks for a ``next`` parameter in the query string.
     558    * Django looks for a ``next`` parameter in the query string or POST data.
    559559    * If that doesn't exist, or is empty, Django tries the URL in the
    560560      ``Referer`` header.
    561561    * If that's empty -- say, if a user's browser suppresses that header --
     
    563563
    564564Here's example HTML template code::
    565565
    566     <form action="/i18n/setlang/" method="get">
     566    <form action="/i18n/setlang/" method="post">
    567567    <input name="next" type="hidden" value="/next/page/" />
    568568    <select name="language">
    569569    {% for lang in LANGUAGES %}
Back to Top