Fix Accept-Language q parsing in locale resolver
All checks were successful
CI / test-and-quality (push) Successful in 3m35s
CI / test-and-quality (pull_request) Successful in 3m38s

This commit is contained in:
2026-03-02 00:38:34 +00:00
parent a0a1424e90
commit c34a52e83e
2 changed files with 26 additions and 2 deletions

View File

@@ -34,14 +34,27 @@ def lobby_i18n_error_messages() -> dict:
return lobby_i18n_catalog().get("backend", {}).get("errors", {})
def _quality_value(language_candidate: str) -> float | None:
for parameter in language_candidate.split(";")[1:]:
key, separator, value = parameter.partition("=")
if separator and key.strip().lower() == "q":
try:
return float(value.strip())
except ValueError:
return None
return None
def resolve_locale(request: HttpRequest) -> str:
default_locale, supported_locales = i18n_locale_config()
accept_language = request.META.get("HTTP_ACCEPT_LANGUAGE") or ""
for candidate in accept_language.split(","):
tag, _sep, quality = candidate.partition(";")
if "q=0" in quality.replace(" ", ""):
quality = _quality_value(candidate)
if quality is not None and quality <= 0:
continue
tag = candidate.split(";", 1)[0]
normalized = tag.strip().replace("_", "-").split("-", 1)[0].lower()
if normalized in supported_locales:
return normalized