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

Public Member Functions

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

Static Public Attributes

babel locale .Locale | None

Detailed Description

Container to assemble client prefferences and settings.

Definition at line 339 of file preferences.py.

Constructor & Destructor Documentation

◆ __init__()

searx.preferences.ClientPref.__init__ ( self,
babel.Locale | None locale = None )

Definition at line 347 of file preferences.py.

347 def __init__(self, locale: babel.Locale | None = None):
348 self.locale = locale
349

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 360 of file preferences.py.

360 def from_http_request(cls, http_request: SXNG_Request):
361 """Build ClientPref object from HTTP request.
362
363 - `Accept-Language used for locale setting
364 <https://www.w3.org/International/questions/qa-accept-lang-locales.en>`__
365
366 """
367 al_header = http_request.headers.get("Accept-Language")
368 if not al_header:
369 return cls(locale=None)
370
371 pairs: list[tuple[babel.Locale, float]] = []
372 for l in al_header.split(','):
373 # fmt: off
374 lang, qvalue = [_.strip() for _ in (l.split(';') + ['q=1',])[:2]]
375 # fmt: on
376 try:
377 qvalue = float(qvalue.split('=')[-1])
378 locale = babel.Locale.parse(lang, sep='-')
379 except (ValueError, babel.core.UnknownLocaleError):
380 continue
381 pairs.append((locale, qvalue))
382
383 locale = None
384 if pairs:
385 pairs.sort(reverse=True, key=lambda x: x[1])
386 locale = pairs[0][0]
387 return cls(locale=locale)
388
389

◆ locale_tag()

searx.preferences.ClientPref.locale_tag ( self)

Definition at line 351 of file preferences.py.

351 def locale_tag(self):
352 if self.locale is None:
353 return None
354 tag = self.locale.language
355 if self.locale.territory:
356 tag += '-' + self.locale.territory
357 return tag
358

References locale.

Member Data Documentation

◆ locale

babel searx.preferences.ClientPref.locale .Locale | None
static

Definition at line 344 of file preferences.py.

Referenced by __init__(), and locale_tag().


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