Skip to content

Commit

Permalink
Add some docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor committed Jan 14, 2025
1 parent 03e4c8f commit ca41e81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "streamlit-gettext"
version = "0.0.3"
version = "0.0.4"
description = "A Streamlit extension for easy localization of your apps"
authors = [{name = "insolor", email = "insolor@gmail.com"}]
readme = "README.md"
Expand Down
15 changes: 15 additions & 0 deletions streamlit_gettext/streamlit_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@


def get_preferred_languages() -> list[str]:
"""
Get preferred languages from the browser
"""
accept_language = st.context.headers.get("Accept-Language") or ""
return re.findall(r"([a-zA-Z-]{2,})", accept_language) or []

Expand All @@ -36,17 +39,29 @@ def translation(self, languages: Iterable[str] | None = None) -> _gettext_module
)

def gettext(self, message: str) -> str:
"""
Get the translation of a message
"""
translation = self.translation(get_preferred_languages())
return translation.gettext(message)

def ngettext(self, singular: str, plural: str, n: int) -> str:
"""
Get the plural form of a message
"""
translation = self.translation(get_preferred_languages())
return translation.ngettext(singular, plural, n)

def pgettext(self, context: str, message: str) -> str:
"""
Get the translation of a message with a context
"""
translation = self.translation(get_preferred_languages())
return translation.pgettext(context, message)

def npgettext(self, context: str, singular: str, plural: str, n: int) -> str:
"""
Get the plural form of a message with a context
"""
translation = self.translation(get_preferred_languages())
return translation.npgettext(context, singular, plural, n)

0 comments on commit ca41e81

Please sign in to comment.