Skip to content

Commit

Permalink
Get languages from websocket headers
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor committed Jul 21, 2024
1 parent f3c8c70 commit a50148e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 48 deletions.
15 changes: 4 additions & 11 deletions package_build/i18n.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import gettext
import time
import re
from pathlib import Path

import streamlit as st
from streamlit_javascript import st_javascript
from streamlit.web.server.websocket_headers import _get_websocket_headers


def get_preferred_languages() -> list[str]:
with st.empty():
while True:
languages = st_javascript("window.navigator.languages")

if languages:
return languages

time.sleep(0.1)
headers = _get_websocket_headers()
return re.findall(r"([a-zA-Z-]{2,})", headers["Accept-Language"]) or []


locale_dir = Path(__file__).parent / "locale"
Expand Down
58 changes: 22 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ python = "^3.10"
streamlit = "^1.36.0"
requests = "^2.32.3"
pydantic = "^2.7.4"
streamlit-javascript = "^0.1.5"

[tool.poetry.group.dev.dependencies]
ruff = "^0.5.0"
Expand Down

0 comments on commit a50148e

Please sign in to comment.