.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.preferences.ClientPref Class Reference
+ Collaboration diagram for searx.preferences.ClientPref:

Public Member Functions

 __init__ (self, Optional[babel.Locale] locale=None)
 
 locale_tag (self)
 
 from_http_request (cls, flask.Request http_request)
 

Static Public Attributes

babel locale .Locale
 

Detailed Description

Container to assemble client prefferences and settings.

Definition at line 322 of file preferences.py.

Constructor & Destructor Documentation

◆ __init__()

searx.preferences.ClientPref.__init__ ( self,
Optional[babel.Locale] locale = None )

Definition at line 330 of file preferences.py.

330 def __init__(self, locale: Optional[babel.Locale] = None):
331 self.locale = locale
332

References searx.preferences.ClientPref.locale, and searx.search.models.SearchQuery.locale.

Member Function Documentation

◆ from_http_request()

searx.preferences.ClientPref.from_http_request ( cls,
flask.Request http_request )
Build ClientPref object from HTTP request.

- `Accept-Language used for locale setting
  <https://www.w3.org/International/questions/qa-accept-lang-locales.en>`__

Definition at line 343 of file preferences.py.

343 def from_http_request(cls, http_request: flask.Request):
344 """Build ClientPref object from HTTP request.
345
346 - `Accept-Language used for locale setting
347 <https://www.w3.org/International/questions/qa-accept-lang-locales.en>`__
348
349 """
350 al_header = http_request.headers.get("Accept-Language")
351 if not al_header:
352 return cls(locale=None)
353
354 pairs = []
355 for l in al_header.split(','):
356 # fmt: off
357 lang, qvalue = [_.strip() for _ in (l.split(';') + ['q=1',])[:2]]
358 # fmt: on
359 try:
360 qvalue = float(qvalue.split('=')[-1])
361 locale = babel.Locale.parse(lang, sep='-')
362 except (ValueError, babel.core.UnknownLocaleError):
363 continue
364 pairs.append((locale, qvalue))
365
366 locale = None
367 if pairs:
368 pairs.sort(reverse=True, key=lambda x: x[1])
369 locale = pairs[0][0]
370 return cls(locale=locale)
371
372

◆ locale_tag()

searx.preferences.ClientPref.locale_tag ( self)

Definition at line 334 of file preferences.py.

334 def locale_tag(self):
335 if self.locale is None:
336 return None
337 tag = self.locale.language
338 if self.locale.territory:
339 tag += '-' + self.locale.territory
340 return tag
341

References searx.preferences.ClientPref.locale, and searx.search.models.SearchQuery.locale.

Member Data Documentation

◆ locale

babel searx.preferences.ClientPref.locale .Locale
static

The documentation for this class was generated from the following file: