#24122: Redirect to translated url after setting language
-------------------------------------+-------------------------------------
     Reporter:  gbdlin               |      Owner:  nobody
         Type:  Uncategorized        |     Status:  new
    Component:                       |    Version:  1.7
  Internationalization               |   Keywords:  set_language,
     Severity:  Normal               |  i18n_patterns, translating urls
 Triage Stage:  Unreviewed           |  Has patch:  1
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 When user change language using set_language view, we can redirect him
 either to url from 'next' post value or 'HTTP_REFERER' header. But if
 i18n_patterns is in use or urls are translated, user will be redirected to
 old url (if no next url) or we must provide new, translated url into next
 value which involves javascript when we want to have dropdown to select
 new language. This can cause:
  - language not changed if i18n_patterns is in use,
  - redirect to 404 or different page when urls are translated.

 I'm proposing patch to translate url taken from 'next' or 'HTTP_REFERER'
 into new language:


 {{{
 def set_language(request):
     """
     Redirect to a given url while setting the chosen language in the
     session or cookie. The url and the language code need to be
     specified in the request parameters.

     Since this view changes how the user will see the rest of the site, it
 must
     only be accessed as a POST request. If called as a GET request, it
 will
     redirect to the page in the request (the 'next' parameter) without
 changing
     any state.
     """
     next = request.POST.get('next', request.GET.get('next'))
     if not is_safe_url(url=next, host=request.get_host()):
         next = request.META.get('HTTP_REFERER')
         if not is_safe_url(url=next, host=request.get_host()):
             next = '/'
     response = http.HttpResponseRedirect(next)
     if request.method == 'POST':
         lang_code = request.POST.get('language', None)
         if lang_code and check_for_language(lang_code):
             parsed = urlsplit(next)
             try:
                 match = resolve(parsed.path)
             except Resolver404:
                 pass
             else:
                 activate(lang_code)
                 try:
                     url = reverse("%s:%s" % (match.namespace,
 match.url_name) if match.namespace else match.url_name, args=match.args,
 kwargs=match.kwargs)
                 except NoReverseMatch:
                     pass
                 else:
                     next = urlunsplit((parsed.scheme, parsed.netloc, url,
 parsed.query, parsed.fragment))
                     response = http.HttpResponseRedirect(next)
             if hasattr(request, 'session'):
                 request.session[LANGUAGE_SESSION_KEY] = lang_code
             else:
                 response.set_cookie(settings.LANGUAGE_COOKIE_NAME,
 lang_code,
                                     max_age=settings.LANGUAGE_COOKIE_AGE,
                                     path=settings.LANGUAGE_COOKIE_PATH,
 domain=settings.LANGUAGE_COOKIE_DOMAIN)
     return response
 }}}

 I'm not sure if this is an bug report or new feature (for me, redirecting
 to 404 or not changing language is definetly a bug) so I left it
 uncategorized. [[BR]]
 Fork on github: https://github.com/gbdlin/django [[BR]]
 Old simmillar (not sure if same case, because of not much explanation from
 author) ticket: https://code.djangoproject.com/ticket/17569 [[BR]]

--
Ticket URL: <https://code.djangoproject.com/ticket/24122>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.b395f66763a604d7cd48ec66be139c90%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to