Ticket #3651: set_language_patch.diff
File set_language_patch.diff, 3.0 KB (added by , 18 years ago) |
---|
-
django/views/i18n.py
9 9 """ 10 10 Redirect to a given url while setting the chosen language in the 11 11 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. 13 16 """ 14 lang_code = request.GET['language'] 15 next = request.GET.get('next', None) 17 next = request.REQUEST.get('next', None) 16 18 if not next: 17 19 next = request.META.get('HTTP_REFERER', None) 18 20 if not next: 19 21 next = '/' 20 22 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) 26 30 return response 27 31 28 32 NullSource = """ -
docs/i18n.txt
547 547 548 548 (Note that this example makes the view available at ``/i18n/setlang/``.) 549 549 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 view552 saves the language choice in the user's session. Otherwise, it saves the 553 language choice in a ``django_language`` cookie.550 The view expects to be called via the ``POST`` method, with a ``language`` 551 parameter set in the query string or POST data. If session support is enabled, 552 the view saves the language choice in the user's session. Otherwise, it saves 553 the language choice in a ``django_language`` cookie. 554 554 555 555 After setting the language choice, Django redirects the user, following this 556 556 algorithm: 557 557 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. 559 559 * If that doesn't exist, or is empty, Django tries the URL in the 560 560 ``Referer`` header. 561 561 * If that's empty -- say, if a user's browser suppresses that header -- … … 563 563 564 564 Here's example HTML template code:: 565 565 566 <form action="/i18n/setlang/" method=" get">566 <form action="/i18n/setlang/" method="post"> 567 567 <input name="next" type="hidden" value="/next/page/" /> 568 568 <select name="language"> 569 569 {% for lang in LANGUAGES %}