[Django] #35034: In some cases i18n set_language does not change url language

17 views
Skip to first unread message

Django

unread,
Dec 13, 2023, 11:23:41 AM12/13/23
to django-...@googlegroups.com
#35034: In some cases i18n set_language does not change url language
------------------------------------------------+------------------------
Reporter: Eric Soroos | Owner: nobody
Type: Bug | Status: new
Component: Internationalization | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Related to #29281, but this time with repro

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.

The patch for `translate_urls` 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:
...
}}}

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

Django

unread,
Dec 13, 2023, 11:40:00 AM12/13/23
to django-...@googlegroups.com
#35034: In some cases i18n set_language does not change url language
-------------------------------------+-------------------------------------
Reporter: EricSoroos | Owner: nobody
Type: Bug | Status: new
Component: | Version: 4.2
Internationalization |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by EricSoroos:

Old description:

New description:

...

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/35034#comment:1>

Django

unread,
Dec 13, 2023, 12:14:20 PM12/13/23
to django-...@googlegroups.com

Old description:

New description:

Related to #29281, but this time with repro. Possibly also related to
#26556.

...

--

--
Ticket URL: <https://code.djangoproject.com/ticket/35034#comment:2>

Django

unread,
Dec 13, 2023, 11:46:05 PM12/13/23
to django-...@googlegroups.com
#35034: In some cases i18n set_language does not change url language
-------------------------------------+-------------------------------------
Reporter: Eric Soroos | Owner: nobody
Type: Bug | Status: closed
Component: | Version: 4.2
Internationalization |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* has_patch: 1 => 0
* resolution: => duplicate


Comment:

Duplicate of #28567.

--
Ticket URL: <https://code.djangoproject.com/ticket/35034#comment:3>

Django

unread,
Dec 14, 2023, 4:05:33 AM12/14/23
to django-...@googlegroups.com
#35034: In some cases i18n set_language does not change url language
-------------------------------------+-------------------------------------
Reporter: Eric Soroos | Owner: nobody
Type: Bug | Status: closed
Component: | Version: 4.2
Internationalization |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Eric Soroos):

I don't think that this is a strict duplicate, since
`prefix_default_language` doesn't enter into the code path.

The issue is when the current language cookie and the current url are out
of sync. This comment:
https://code.djangoproject.com/ticket/28567#comment:14 does have the
essential issue though -- `translate_url` only only functions on urls from
the current language.

--
Ticket URL: <https://code.djangoproject.com/ticket/35034#comment:4>

Django

unread,
Dec 14, 2023, 4:10:54 AM12/14/23
to django-...@googlegroups.com
#35034: In some cases i18n set_language does not change url language
-------------------------------------+-------------------------------------
Reporter: Eric Soroos | Owner: nobody
Type: Bug | Status: closed
Component: | Version: 4.2
Internationalization |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

As far as I'm aware, your issue in described in the
https://code.djangoproject.com/ticket/28567#comment:5.

> This comment: https://code.djangoproject.com/ticket/28567#comment:14
does have the essential issue though

Feel-free to add a comment to the #28567.

--
Ticket URL: <https://code.djangoproject.com/ticket/35034#comment:5>

Reply all
Reply to author
Forward
0 new messages