Opened 4 years ago
Last modified 4 years ago
#32922 closed New feature
Adding "preferred_language" property to "HttpRequest" object — at Initial Version
| Reported by: | Ruslan Semagin | Owned by: | nobody | 
|---|---|---|---|
| Component: | HTTP handling | Version: | 3.2 | 
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
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 '*'
      
  Note:
 See   TracTickets
 for help on using tickets.