[Django] #32922: Adding "preferred_language" property to "HttpRequest" object

3 views
Skip to first unread message

Django

unread,
Jul 13, 2021, 8:55:11 AM7/13/21
to django-...@googlegroups.com
#32922: Adding "preferred_language" property to "HttpRequest" object
-----------------------------------------+------------------------
Reporter: pixel365 | Owner: nobody
Type: New feature | Status: new
Component: HTTP handling | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Sometimes it is useful to get the client's preferred language from the
request and then pass it somewhere, for example, apply a filter to fetch
data.
Therefore, it is useful to access this property in the object.

{{{
@property
def preferred_language(self):
"""Returns the client's language in order of priority from the
"Accept-Language" header"""
languages = [_.strip() for _ in self.headers.get('Accept-Language',
'*').split(',')]
for language in languages:
if language.split(';')[0] == language:
return language
return language.split(';')[0]
return '*'
}}}

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

Django

unread,
Jul 13, 2021, 9:16:40 AM7/13/21
to django-...@googlegroups.com
#32922: Adding "preferred_language" property to "HttpRequest" object
-------------------------------+--------------------------------------

Reporter: pixel365 | Owner: nobody
Type: New feature | Status: new
Component: HTTP handling | Version: 3.2
Severity: Normal | Resolution:

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

Old description:

> Sometimes it is useful to get the client's preferred language from the
> request and then pass it somewhere, for example, apply a filter to fetch
> data.
> Therefore, it is useful to access this property in the object.
>

>
> {{{
> @property
> def preferred_language(self):
> """Returns the client's language in order of priority from the
> "Accept-Language" header"""
> languages = [_.strip() for _ in self.headers.get('Accept-Language',
> '*').split(',')]
> for language in languages:
> if language.split(';')[0] == language:
> return language
> return language.split(';')[0]
> return '*'
> }}}

New description:

Sometimes it is useful to get the client's preferred language from the
request and then pass it somewhere, for example, apply a filter to fetch
data.
Therefore, it is useful to access this property in the object.

{{{
@property
def preferred_language(self):
"""Returns the client's language in order of priority from the
"Accept-Language" header"""
languages = [_.strip() for _ in self.headers.get('Accept-Language',
'*').split(',')]

return languages[0].split(';')[0]
}}}

--

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

Django

unread,
Jul 13, 2021, 5:49:44 PM7/13/21
to django-...@googlegroups.com
#32922: Adding "preferred_language" property to "HttpRequest" object
--------------------------------+--------------------------------------
Reporter: Ruslan Semagin | Owner: nobody
Type: New feature | Status: closed

Component: HTTP handling | Version: 3.2
Severity: Normal | Resolution: wontfix

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 Nick Pope):

* status: new => closed
* resolution: => wontfix


Comment:

So unfortunately this is not as simple as what you have proposed for a
number of reasons:

- The `q` value for the priority of entries in the header should be taken
into account
[https://github.com/django/django/pull/14632#pullrequestreview-705530998
as mentioned by Keryn].
- The special `*` value shouldn't be returned as it isn't a valid
language.
- The value in `Accept-Language` may not be the user's "preferred"
language. It is just the languages that the client is configured to
accept.

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-
Language for more details.

The good news is that Django has already implemented this and taken
everything into consideration. Check out
[https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.translation.get_language_from_request
get_language_from_request()].

Obviously this restricts the returned languages to those that are
available in `settings.LANGUAGES`.

If you really need to have the "preferred" and unadulterated value from
the `Accept-Language` header, you could use the
`parse_accept_lang_header()` function, but beware that this is an
undocumented, private function.

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

Reply all
Reply to author
Forward
0 new messages