Files
duty-teller/duty_teller/i18n/core.py
Nikolay Tatarinov 67ba9826c7 feat: unify language handling across the application
- Updated the language configuration to use a single source of truth from `DEFAULT_LANGUAGE` for the bot, API, and Mini App, eliminating auto-detection from user settings.
- Refactored the `get_lang` function to always return `DEFAULT_LANGUAGE`, ensuring consistent language usage throughout the application.
- Modified the handling of language in various components, including API responses and UI elements, to reflect the new language management approach.
- Enhanced documentation and comments to clarify the changes in language handling.
- Added unit tests to verify the new language handling behavior and ensure coverage for the updated functionality.
2026-03-02 23:05:28 +03:00

36 lines
1.1 KiB
Python

"""get_lang and t(): language from config (DEFAULT_LANGUAGE), translate by key with fallback to en."""
from typing import TYPE_CHECKING
import duty_teller.config as config
from duty_teller.i18n.messages import MESSAGES
if TYPE_CHECKING:
from telegram import User
def get_lang(user: "User | None") -> str:
"""
Return the application language: always config.DEFAULT_LANGUAGE.
The user argument is kept for backward compatibility but is ignored.
The whole deployment uses a single language from DEFAULT_LANGUAGE.
"""
return config.DEFAULT_LANGUAGE
def t(lang: str, key: str, **kwargs: str) -> str:
"""
Return translated string for lang and key; substitute kwargs into placeholders like {phone}.
Fallback to 'en' if key missing for lang.
"""
lang = "ru" if lang == "ru" else "en"
messages = MESSAGES.get(lang) or MESSAGES["en"]
template = messages.get(key)
if template is None:
template = MESSAGES["en"].get(key, key)
try:
return template.format(**kwargs)
except KeyError:
return template