#28567: Incompatibility between the set_language() view, LocaleMiddleware, and
i18n_patterns() when prefix_default_language=False.
--------------------------------------+------------------------------------
     Reporter:  George Tantiras       |                    Owner:  (none)
         Type:  Bug                   |                   Status:  new
    Component:  Internationalization  |                  Version:  dev
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by Eric Soroos):

 #35034 was marked as a duplicate -- however it's a little different, with
 no relationship to the `prefix_default_language`:

 When posting to `/i18n/setlang/`:
  * with a referrer in a language that's not the current django cooke
 language like `/km/admin/foo/bar`,
  * with a current language cookie of `django_language=en`
  * and a post content of `next=&language=en`

 `django.urls.translate_urls` attempts to resolve the url using the current
 request language (in this case, en) and fails, because the url is in a
 different language (km).

 `i18n.set_language` then returns the original referrer in the location
 header of the redirect, and the language doesn't apparently change,
 confusing the user.

 This is approximately option 2 from
 https://code.djangoproject.com/ticket/28567#comment:14, but with the
 resolution entirely in `translate_urls` instead of `set_language`. The
 patch for `translate_urls` to handle alternate locales is approximately:

 {{{
 #!python
 from django.utils.translation import override, check_for_language,
 get_language_from_path

 ...

 def translate_url(url, lang_code):
     """
     Given a URL (absolute or relative), try to get its translated version
 in
     the `lang_code` language (either by i18n_patterns or by translated
 regex).
     Return the original URL if no translated version is found.
     """
     parsed = urlsplit(url)
     try:
         # URL may be encoded.
         try:
             match = resolve(unquote(parsed.path))
         except Resolver404:
             url_lang_code = get_language_from_path(unquote(parsed.path))
             if url_lang_code and check_for_language(url_lang_code):
                 with override(url_lang_code):
                     match = resolve(unquote(parsed.path))
             else:
                 raise
     except Resolver404:
         pass
     else:
 ...
 }}}

 https://github.com/django/django/compare/main...EricSoroos:django:35034
 -translate-url

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28567#comment:20>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018c6797c135-5a73b4d4-0d46-4763-a59e-8f817258dd73-000000%40eu-central-1.amazonses.com.

Reply via email to