[Django] #34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE

40 views
Skip to first unread message

Django

unread,
Oct 2, 2022, 6:46:37 AM10/2/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
sergioisidoro |
Type: | Status: new
Cleanup/optimization |
Component: | Version: 4.1
Internationalization | Keywords: i18n
Severity: Normal | get_language_from_request
Triage Stage: | middleware
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
I'm writing a middleware to fallback to a different language depending on
the TLD of the domain of the HTTP_HOST

However, I noticed that `get_language_from_request` falls back to the
settings default language, which will almost always take precedence in
this case.

This is quite confusing, since `settings.LANGUAGE_CODE` is not "from the
request", but from the application configuration, and it feels that the
responsibility of falling back to the default language should lie in the
Middleware, not in this function.

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

Django

unread,
Oct 2, 2022, 6:50:52 AM10/2/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage:
get_language_from_request | Unreviewed
middleware |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by sergioisidoro:

Old description:

> I'm writing a middleware to fallback to a different language depending on
> the TLD of the domain of the HTTP_HOST
>
> However, I noticed that `get_language_from_request` falls back to the
> settings default language, which will almost always take precedence in
> this case.
>
> This is quite confusing, since `settings.LANGUAGE_CODE` is not "from the
> request", but from the application configuration, and it feels that the
> responsibility of falling back to the default language should lie in the
> Middleware, not in this function.

New description:

I'm writing a middleware to fallback to a different language depending on
the TLD of the domain of the HTTP_HOST

However, I noticed that `get_language_from_request` falls back to the
settings default language, which will almost always take precedence in
this case.

This is quite confusing, since `settings.LANGUAGE_CODE` is not "from the
request", but from the application configuration, and it feels that the
responsibility of falling back to the default language should lie in the
Middleware, not in this function.

Solution / Summary: `get_language_from_request` should return None, to
communicate to the middleware that there was no language from request, and
that the middleware should fallback to the default. Otherwise if the
`get_language_from_request` returns "EN" we don't know if "EN" is actually
a request preference, or because it came from the default
`settings.LANGUAGE_CODE`

--

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

Django

unread,
Oct 2, 2022, 7:48:10 AM10/2/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage:
get_language_from_request | Unreviewed
middleware |
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


Comment:

Here's a draft PR for this issue:
https://github.com/django/django/pull/16142

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

Django

unread,
Oct 2, 2022, 12:12:59 PM10/2/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage: Accepted
get_language_from_request |

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

* stage: Unreviewed => Accepted


Comment:

Reading the rationale and looking at the draft PR, I'm rather sympathetic
to the idea.

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

Django

unread,
Oct 4, 2022, 8:49:04 AM10/4/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner: Aman
Type: | Pandey
Cleanup/optimization | Status: assigned

Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage: Accepted
get_language_from_request |
middleware |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aman Pandey):

* owner: nobody => Aman Pandey
* status: new => assigned


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

Django

unread,
Oct 5, 2022, 11:51:37 PM10/5/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner: Aman
Type: | Pandey
Cleanup/optimization | Status: assigned
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage: Accepted
get_language_from_request |
middleware |
Has patch: 1 | Needs documentation: 1

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

* needs_docs: 0 => 1


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

Django

unread,
Oct 6, 2022, 6:19:05 AM10/6/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner:
Type: | sergioisidoro

Cleanup/optimization | Status: assigned
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage: Accepted
get_language_from_request |
middleware |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

* owner: Aman Pandey => sergioisidoro


Comment:

Hi Aman,

Sorry the ticket is already being worked on by sergioisidoro :)

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

Django

unread,
Oct 8, 2022, 6:39:59 AM10/8/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner:
Type: | sergioisidoro
Cleanup/optimization | Status: assigned
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage: Accepted
get_language_from_request |
middleware |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Aman Pandey):

Replying to [comment:6 David Sanders]:


> Hi Aman,
>
> Sorry the ticket is already being worked on by sergioisidoro :)

ohh it's fine , my bad i took it without noticing .

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

Django

unread,
Oct 13, 2022, 3:49:27 AM10/13/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner:
Type: | sergioisidoro
Cleanup/optimization | Status: assigned
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution:
Keywords: i18n | Triage Stage: Ready for
get_language_from_request | checkin
middleware |
Has patch: 1 | Needs documentation: 0

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

* needs_docs: 1 => 0
* stage: Accepted => Ready for checkin


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

Django

unread,
Oct 13, 2022, 5:04:25 AM10/13/22
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner:
Type: | sergioisidoro
Cleanup/optimization | Status: closed
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution: fixed

Keywords: i18n | Triage Stage: Ready for
get_language_from_request | checkin
middleware |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"94e7f471c4edef845a4fe5e3160132997b4cca81" 94e7f47]:
{{{
#!CommitTicketReference repository=""
revision="94e7f471c4edef845a4fe5e3160132997b4cca81"
Fixed #34069 -- Made LocaleMiddleware respect language from requests when
i18n patterns are used.
}}}

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

Django

unread,
May 2, 2023, 12:06:36 AM5/2/23
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner:
Type: | sergioisidoro
Cleanup/optimization | Status: closed
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution: duplicate

Keywords: i18n | Triage Stage:
get_language_from_request | Unreviewed
middleware |
Has patch: 0 | Needs documentation: 0

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

* has_patch: 1 => 0
* resolution: fixed => duplicate
* easy: 1 => 0
* stage: Ready for checkin => Unreviewed


Comment:

Duplicate of #29425, see [https://forum.djangoproject.com/t/django-
behavior-on-translatable-url-patterns-i18n-patterns-when-prefix-default-
language-false-is-used/20564/1 discussion].

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

Django

unread,
May 2, 2023, 12:33:08 AM5/2/23
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner:
Type: | sergioisidoro
Cleanup/optimization | Status: closed
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution: duplicate
Keywords: i18n | Triage Stage:
get_language_from_request | Unreviewed
middleware |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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

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/34069#comment:11>

Django

unread,
May 2, 2023, 12:33:59 AM5/2/23
to django-...@googlegroups.com
#34069: get_language_from_request should not fallback to settings.LANGUAGE_CODE
-------------------------------------+-------------------------------------
Reporter: sergioisidoro | Owner:
Type: | sergioisidoro
Cleanup/optimization | Status: closed
Component: | Version: 4.1
Internationalization |
Severity: Normal | Resolution: duplicate
Keywords: i18n | Triage Stage:
get_language_from_request | Unreviewed
middleware |
Has patch: 0 | 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/34069#comment:12>

Reply all
Reply to author
Forward
0 new messages