Add internationalization support and enhance language handling
All checks were successful
CI / lint-and-test (push) Successful in 14s
All checks were successful
CI / lint-and-test (push) Successful in 14s
- Introduced a new i18n module for managing translations and language normalization, supporting both Russian and English. - Updated various handlers and services to utilize the new translation functions for user-facing messages, improving user experience based on language preferences. - Enhanced error handling and response messages to be language-aware, ensuring appropriate feedback is provided to users in their preferred language. - Added tests for the i18n module to validate language detection and translation functionality. - Updated the example environment file to include a default language configuration.
This commit is contained in:
@@ -12,6 +12,14 @@ load_dotenv()
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def _normalize_default_language(value: str) -> str:
|
||||
"""Normalize DEFAULT_LANGUAGE from env to 'ru' or 'en'."""
|
||||
if not value:
|
||||
return "en"
|
||||
v = value.strip().lower()
|
||||
return "ru" if v.startswith("ru") else "en"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
"""Optional injectable settings built from env. Tests can override or build from env."""
|
||||
@@ -27,6 +35,7 @@ class Settings:
|
||||
cors_origins: list[str]
|
||||
external_calendar_ics_url: str
|
||||
duty_display_tz: str
|
||||
default_language: str
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "Settings":
|
||||
@@ -62,6 +71,9 @@ class Settings:
|
||||
).strip(),
|
||||
duty_display_tz=os.getenv("DUTY_DISPLAY_TZ", "Europe/Moscow").strip()
|
||||
or "Europe/Moscow",
|
||||
default_language=_normalize_default_language(
|
||||
os.getenv("DEFAULT_LANGUAGE", "en").strip()
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -95,6 +107,9 @@ EXTERNAL_CALENDAR_ICS_URL = os.getenv("EXTERNAL_CALENDAR_ICS_URL", "").strip()
|
||||
DUTY_DISPLAY_TZ = (
|
||||
os.getenv("DUTY_DISPLAY_TZ", "Europe/Moscow").strip() or "Europe/Moscow"
|
||||
)
|
||||
DEFAULT_LANGUAGE = _normalize_default_language(
|
||||
os.getenv("DEFAULT_LANGUAGE", "en").strip()
|
||||
)
|
||||
|
||||
|
||||
def is_admin(username: str) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user