aiogram/aiogram/utils/i18n/context.py
Alex Root Junior e4046095d7
Dev 3.x i18n & improvements (#696)
* Added base code and make code improvements
* Auto-exclude coverage for `if TYPE_CHECKING:`
* Fixed current coverage
* Cover I18n module
* Update pipeline
* Fixed annotations
* Added docs
* Move exceptions
* Added tests for KeyboardBuilder and initial docs
* Remove help generator (removed from sources tree, requires rewrite)
* Added patch-notes #698, #699, #700, #701, #702, #703
2021-09-22 00:52:38 +03:00

26 lines
642 B
Python

from contextvars import ContextVar
from typing import Any, Optional
from aiogram.utils.i18n.core import I18n
from aiogram.utils.i18n.lazy_proxy import LazyProxy
ctx_i18n: ContextVar[Optional[I18n]] = ContextVar("aiogram_ctx_i18n", default=None)
def get_i18n() -> I18n:
i18n = ctx_i18n.get()
if i18n is None:
raise LookupError("I18n context is not set")
return i18n
def gettext(*args: Any, **kwargs: Any) -> str:
return get_i18n().gettext(*args, **kwargs)
def lazy_gettext(*args: Any, **kwargs: Any) -> LazyProxy:
return LazyProxy(gettext, *args, **kwargs)
ngettext = gettext
lazy_ngettext = lazy_gettext