Re: [Django] #34515: Translatable URL patterns raise 404 for non-English default language when prefix_default_language=False is used.

35 views
Skip to first unread message

Django

unread,
Apr 25, 2023, 4:55:56 AM4/25/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: nobody
Type: Bug | Status: new
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Mariusz Felisiak (removed)


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

Django

unread,
Apr 25, 2023, 5:11:41 AM4/25/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: nobody
Type: Bug | Status: new
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:1 ab]:
> Expected behavior:
[https://docs.djangoproject.com/en/4.2/topics/i18n/translation/#how-
django-discovers-language-preference django 4.2 documentation]
>
> LocaleMiddleware tries to determine the user’s language preference by
following this algorithm:
>
> - First, it looks for the language prefix in the requested URL. This is
only performed when you are using the i18n_patterns function in your root
URLconf. See Internationalization: in URL patterns for more information
about the language prefix and how to internationalize URL patterns.
>
> - Failing that, it looks for a cookie. The name of the cookie used is
set by the LANGUAGE_COOKIE_NAME setting. (The default name is
django_language.)
>
> - Failing that, it looks at the Accept-Language HTTP header. This header
is sent by your browser and tells the server which language(s) you prefer,
in order by priority. Django tries each language in the header until it
finds one with available translations.
>
> - Failing that, it uses the global LANGUAGE_CODE setting.

IMO it still works that way. However, in Django 4.2
`get_language_from_request()` returns the language from a request (`en`
for me) which is activated and the default path `about/` is no longer
translated to the `a-propos/`. This is definitely a change from the
previous behavior.

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

Django

unread,
Apr 25, 2023, 6:00:06 AM4/25/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: nobody
Type: Bug | Status: new
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ab):

Replying to [comment:4 Mariusz Felisiak]:


>
> IMO it still works that way. However, in Django 4.2
`get_language_from_request()` returns the language from a request (`en`
for me) which is activated and the default path `about/` is no longer
translated to the `a-propos/`. This is definitely a change from the
previous behavior.

Thank you Mariusz for the quick reaction.

I agree it still globally works that way, nevertheless, in the case I
describe, when django looks for the language prefix in the requested URL
and there is not language prefix, I would expect django to return "fr",
not to go to the next steps of the algorithm. Because I want
`prefix_default_language = False` to take precedence on cookies or
headers. Does it make sense?

I need to add that I use `translate_url` to
[https://github.com/aboutofpluto/django-prefix-default-language-bug-
report/blob/master/prefix/urls.py#L11 build the links] in my templates.
Consequently, my URLs are translated in the template **only** (hence the
404).

So you're right about the default path not being translated anymore.

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

Django

unread,
Apr 25, 2023, 6:28:42 AM4/25/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: nobody
Type: Bug | Status: new
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: sergioisidoro (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:6>

Django

unread,
Apr 25, 2023, 8:49:57 AM4/25/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sota Tabu
Type: Bug | Status: assigned

Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sota Tabu):

* owner: nobody => Sota Tabu
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:7>

Django

unread,
Apr 25, 2023, 3:30:52 PM4/25/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce

Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* owner: Sota Tabu => Sarah Boyce


Comment:

I have a PR with what I think the issue is, but not confident
https://github.com/django/django/pull/16797
@ab I think what you're saying makes sense

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:8>

Django

unread,
Apr 26, 2023, 2:13:12 AM4/26/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:9>

Django

unread,
Apr 26, 2023, 2:36:47 AM4/26/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

>I agree it still globally works that way, nevertheless, in the case I
describe, when django looks for the language prefix in the requested URL
and there is not language prefix, I would expect django to return "fr",
not to go to the next steps of the algorithm. Because I want
prefix_default_language = False to take precedence on cookies or headers.
Does it make sense?

My 2¢: Is ignoring the process for determining the language the least
surprising choice here though? It all depends on whether no-prefix URL
should refer to a user setting or the site's default language. I mean imho
navigating to a URL I might expect it to show the language in what I chose
🤷‍♂️

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:10>

Django

unread,
Apr 26, 2023, 3:34:29 AM4/26/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ab):

* keywords: internationalization, i18n, prefix_default_language =>
internationalization, i18n, prefix_default_language, i18n_patterns


Comment:

@Sarah: yes, it is the same problem. After investigating the code, the
change in behavior is linked to the fact that `get_language_from_path`
returns `None` when the url is not prefixed. So, the cookie is used (or
the `Accept-Language` header sent by the browser).

@David: thanks for your contribution, but I do not fully agree. If
`prefix_default_url` is `True`, the language is correctly detected by
django based on the URL. If I set `prefix_default_url` to `False` I expect
the same behavior for the default language without prefix. When I decide
do use `i18n_patterns` at least (I have just added this tag to the
ticket).
When `i18n_patterns`is not used, I agree with you.

So the problem might come from i18n_patterns not calling/handling
correctly the calls to the new `get_language_*` functions.

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:11>

Django

unread,
Apr 26, 2023, 3:42:10 AM4/26/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ab):

@sarah: I'll test your patch because your edits might solve the problem
with HTTP headers too. Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:12>

Django

unread,
Apr 27, 2023, 4:36:04 AM4/27/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ab):

Just to keep track of the current work on this issue, there is a
discussion about how django should behave here:
https://github.com/django/django/pull/16797#issuecomment-1524958085

As suggested by Sarah, I'll post to django-developers for a wider range of
opinions.

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:13>

Django

unread,
Apr 27, 2023, 5:49:25 AM4/27/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

[https://forum.djangoproject.com/t/django-behavior-on-translatable-url-
patterns-i18n-patterns-when-prefix-default-language-false-is-used/20564
Django forum discussion].

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:14>

Django

unread,
Apr 28, 2023, 3:39:33 AM4/28/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Alternative [https://github.com/django/django/pull/16810 PR].

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:15>

Django

unread,
May 2, 2023, 12:33:08 AM5/2/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"0e444e84f87d174713a2aef0c4f9704ce2865586" 0e444e8]:
{{{
#!CommitTicketReference repository=""
revision="0e444e84f87d174713a2aef0c4f9704ce2865586"
Fixed #34515 -- Made LocaleMiddleware prefer language from paths when i18n
patterns are used.

Regression in 94e7f471c4edef845a4fe5e3160132997b4cca81.

This reverts commit 94e7f471c4edef845a4fe5e3160132997b4cca81
(refs #34069) and
partly reverts commit 3b4728310a7a64f8fcc548163b0aa5f98a5c78f5.

Thanks Anthony Baillard for the report.

Co-Authored-By: Sarah Boyce <42296566+...@users.noreply.github.com>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:16>

Django

unread,
May 2, 2023, 12:33:59 AM5/2/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"f200d8369822e418906579503b8d3015a03e75f0" f200d836]:
{{{
#!CommitTicketReference repository=""
revision="f200d8369822e418906579503b8d3015a03e75f0"
[4.2.x] Fixed #34515 -- Made LocaleMiddleware prefer language from paths


when i18n patterns are used.

Regression in 94e7f471c4edef845a4fe5e3160132997b4cca81.

This reverts commit 94e7f471c4edef845a4fe5e3160132997b4cca81
(refs #34069) and
partly reverts commit 3b4728310a7a64f8fcc548163b0aa5f98a5c78f5.

Thanks Anthony Baillard for the report.

Co-Authored-By: Sarah Boyce <42296566+...@users.noreply.github.com>

Backport of 0e444e84f87d174713a2aef0c4f9704ce2865586 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:17>

Django

unread,
May 2, 2023, 3:04:20 AM5/2/23
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by ab):

Thank you for working on this and maintaining django so well!

Anthony

--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:18>

Django

unread,
May 17, 2025, 5:24:45 AM5/17/25
to django-...@googlegroups.com
#34515: Translatable URL patterns raise 404 for non-English default language when
prefix_default_language=False is used.
-------------------------------------+-------------------------------------
Reporter: ab | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: | Version: 4.2
Internationalization |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
internationalization, i18n, |
prefix_default_language, |
i18n_patterns |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Foucauld Degeorges):

Quite logically, this fix recreated the previous bug
[https://code.djangoproject.com/ticket/32886].

A summary of my understanding of the issue:

The root cause seems to be that in an app where the following routes
coexist:

- type A: a language-independant route. The language cannot be inferred
from calling such routes.
- type B: a language-dependant route, with a prefix from `i18n_patterns`,
with `prefix_default_language=False`. The language can be inferred from
calling such routes, and will supersede other sources of language.

`LocaleMiddleware` cannot tell those apart. With the code currently on
master, `LocaleMiddleware` detects that at least one URL of type B exists
in the app, and will assume that all routes of type A or B are of type B.
It will always choose to infer the language.

If I understand correctly, the PR that fixed that other bug, simply
inverted the behaviour and created this bug here. Reverting that PR
recreated the old bug.

The actual fix would be for `LocaleMiddleware` to be able to know whether
the ''specific route'' is of type A or B, and based on that, choose to
infer the language of the request or not.
As a workaround, we have implemented our own `LocaleMiddleware` which:

- uses logic adapted from `django-extensions`' `show_urls` command to list
all routes in all activated languages
- groups them by name to see whether some routes have more than one
distinct pattern
- this tells us whether a route is translated (of type B), or not
translated (of type A)
- if type B, `LocaleMiddleware` should infer the language from the route.
If type A, it shouldn't.
--
Ticket URL: <https://code.djangoproject.com/ticket/34515#comment:19>
Reply all
Reply to author
Forward
0 new messages