Opened 2 years ago

Closed 2 years ago

Last modified 20 months ago

#34069 closed Cleanup/optimization (duplicate)

get_language_from_request should not fallback to settings.LANGUAGE_CODE

Reported by: sergioisidoro Owned by: sergioisidoro
Component: Internationalization Version: 4.1
Severity: Normal Keywords: i18n get_language_from_request middleware
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by sergioisidoro)

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

Change History (12)

comment:1 by sergioisidoro, 2 years ago

Description: modified (diff)

comment:2 by sergioisidoro, 2 years ago

Has patch: set

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

comment:3 by Claude Paroz, 2 years ago

Triage Stage: UnreviewedAccepted

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

comment:4 by Aman Pandey, 2 years ago

Owner: changed from nobody to Aman Pandey
Status: newassigned

comment:5 by Mariusz Felisiak, 2 years ago

Needs documentation: set

comment:6 by David Sanders, 2 years ago

Owner: changed from Aman Pandey to sergioisidoro

Hi Aman,

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

in reply to:  6 comment:7 by Aman Pandey, 2 years ago

Replying to 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 .

comment:8 by Mariusz Felisiak, 2 years ago

Needs documentation: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 94e7f47:

Fixed #34069 -- Made LocaleMiddleware respect language from requests when i18n patterns are used.

comment:10 by Mariusz Felisiak, 20 months ago

Easy pickings: unset
Has patch: unset
Resolution: fixedduplicate
Triage Stage: Ready for checkinUnreviewed

Duplicate of #29425, see discussion.

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 20 months ago

In 0e444e8:

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+sarahboyce@…>

comment:12 by Mariusz Felisiak <felisiak.mariusz@…>, 20 months ago

In f200d836:

[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+sarahboyce@…>

Backport of 0e444e84f87d174713a2aef0c4f9704ce2865586 from main

Note: See TracTickets for help on using tickets.
Back to Top