aboutsummaryrefslogtreecommitdiffstats
path: root/fatcat_scholar/web.py
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@archive.org>2020-10-22 19:01:13 -0700
committerBryan Newbold <bnewbold@archive.org>2020-10-22 19:01:13 -0700
commit08156558afdd37fdf2bb62a97e1814aab1f21945 (patch)
tree25d9a8f878f856f2732b5728eb654345b66ad752 /fatcat_scholar/web.py
parentea28911d14f3996ace13bf95ce1b8f6537d43fcc (diff)
downloadfatcat-scholar-08156558afdd37fdf2bb62a97e1814aab1f21945.tar.gz
fatcat-scholar-08156558afdd37fdf2bb62a97e1814aab1f21945.zip
web: basic Accept-Language HTTP negotiation
Diffstat (limited to 'fatcat_scholar/web.py')
-rw-r--r--fatcat_scholar/web.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py
index 73f79b5..f266e08 100644
--- a/fatcat_scholar/web.py
+++ b/fatcat_scholar/web.py
@@ -15,7 +15,7 @@ import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from fatcat_scholar.config import settings, GIT_REVISION
-from fatcat_scholar.hacks import Jinja2Templates
+from fatcat_scholar.hacks import Jinja2Templates, parse_accept_lang
from fatcat_scholar.search import do_fulltext_search, FulltextQuery, FulltextHits
@@ -49,6 +49,17 @@ class LangPrefix:
def __init__(self, request: Request):
self.prefix: str = ""
self.code: str = settings.I18N_LANG_DEFAULT
+ # first try to parse a language code from header
+ try:
+ accept_code = parse_accept_lang(
+ request.headers.get("accept-language", ""), I18N_LANG_TRANSLATIONS,
+ )
+ if accept_code:
+ self.code = accept_code
+ except Exception:
+ pass
+
+ # then override this with any language code in URL
for lang_option in I18N_LANG_OPTIONS:
if request.url.path.startswith(f"/{lang_option}/"):
self.prefix = f"/{lang_option}"