Skip to content

Commit a7ee394

Browse files
committed
Use streamlit-gettext package
1 parent 72ff1ee commit a7ee394

File tree

4 files changed

+22
-43
lines changed

4 files changed

+22
-43
lines changed

app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from pathlib import Path
22

33
import streamlit as st
4+
from streamlit_gettext import get_preferred_languages
45

56
from package_build.download_parts import download_parts
67
from package_build.file_list import show_file_list
7-
from package_build.i18n import get_preferred_languages
88
from package_build.i18n import gettext as _
99
from package_build.metadata import get_dict_metadata, get_hook_metadata
1010
from package_build.models import DictInfoEntry

package_build/i18n.py

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,9 @@
1-
import gettext as gettext_module
2-
import re
3-
from functools import lru_cache
41
from pathlib import Path
52

6-
import streamlit as st
7-
from loguru import logger
3+
from streamlit_gettext import GettextWrapper
84

5+
current_path = Path(__file__).parent
6+
_gettext_wrapper = GettextWrapper(locale_path=current_path / "locale")
97

10-
def get_preferred_languages() -> list[str]:
11-
accept_language = st.context.headers.get("Accept-Language") or ""
12-
return re.findall(r"([a-zA-Z-]{2,})", accept_language) or []
13-
14-
15-
@lru_cache(maxsize=128, typed=False)
16-
def _get_lang(languages: tuple[str]) -> gettext_module.NullTranslations:
17-
logger.info(f"Languages: {languages}")
18-
locale_dir = Path(__file__).parent / "locale"
19-
20-
return gettext_module.translation(
21-
"messages",
22-
localedir=locale_dir,
23-
languages=languages,
24-
fallback=True,
25-
)
26-
27-
28-
29-
class LanguageWrapper:
30-
@staticmethod
31-
def _get_lang() -> gettext_module.NullTranslations:
32-
return _get_lang(tuple(get_preferred_languages()))
33-
34-
def gettext(self, message: str) -> str:
35-
lang = self._get_lang()
36-
return lang.gettext(message)
37-
38-
def ngettext(self, singular: str, plural: str, n: int) -> str:
39-
lang = self._get_lang()
40-
return lang.ngettext(singular, plural, n)
41-
42-
43-
lang = LanguageWrapper()
44-
gettext = lang.gettext
45-
ngettext = lang.ngettext
8+
gettext = _gettext_wrapper.gettext
9+
ngettext = _gettext_wrapper.ngettext

poetry.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ streamlit = "^1.41.1"
1212
requests = "^2.32.3"
1313
pydantic = "^2.10.4"
1414
loguru = "^0.7.3"
15+
streamlit-gettext = "^0.0.4"
1516

1617
[tool.poetry.group.dev.dependencies]
1718
ruff = "^0.8.4"

0 commit comments

Comments
 (0)