.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.preferences.ClientPref Class Reference

Public Member Functions

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

Static Public Attributes

babel locale .Locale
 

Detailed Description

Container to assemble client prefferences and settings.

Definition at line 325 of file preferences.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 333 of file preferences.py.

333 def __init__(self, locale: Optional[babel.Locale] = None):
334 self.locale = locale
335

References locale.

Member Function Documentation

◆ from_http_request()

searx.preferences.ClientPref.from_http_request ( cls,
SXNG_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 346 of file preferences.py.

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

◆ locale_tag()

searx.preferences.ClientPref.locale_tag ( self)

Definition at line 337 of file preferences.py.

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

References locale.

Member Data Documentation

◆ locale

babel searx.preferences.ClientPref.locale .Locale
static

Definition at line 330 of file preferences.py.

Referenced by __init__(), and locale_tag().


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