Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/language_inline_markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ async def cmd_start(message: Message, i18n: I18nContext):


@router.callback_query(F.data == "back")
async def btn_help(call: CallbackQuery, i18n: I18nContext):
async def btn_back(call: CallbackQuery, i18n: I18nContext):
await call.message.edit_text(
text=i18n.get("hello", user=call.from_user.full_name)
)


@router.callback_query(lang_kb.filter)
async def btn_help(call: CallbackQuery, lang: str, i18n: I18nContext):
async def btn_lang(call: CallbackQuery, lang: str, i18n: I18nContext):
await call.answer()
await i18n.set_locale(locale=lang)
await call.message.edit_text(text=i18n.cur.lang(language=i18n.locale))
Expand Down
4 changes: 2 additions & 2 deletions src/aiogram_i18n/cores/fluent_compile_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import Any, cast

from aiogram_i18n.exceptions import KeyNotFoundError, NoModuleError
from aiogram_i18n.exceptions import FluentMessageError, KeyNotFoundError, NoModuleError
from aiogram_i18n.utils.text_decorator import td

try:
Expand Down Expand Up @@ -44,7 +44,7 @@ def get(self, message: str, locale: str | None = None, /, **kwargs: Any) -> str:
raise KeyNotFoundError(message) from None
return message
if errors:
raise errors[0]
raise FluentMessageError(message_id=message, errors=errors)
return cast(str, text) # 'cause fluent_compiler type-ignored

def find_locales(self) -> dict[str, FluentBundle]:
Expand Down
4 changes: 2 additions & 2 deletions src/aiogram_i18n/cores/fluent_runtime_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import Any, cast

from aiogram_i18n.exceptions import KeyNotFoundError, NoModuleError
from aiogram_i18n.exceptions import FluentMessageError, KeyNotFoundError, NoModuleError
from aiogram_i18n.utils.text_decorator import td

try:
Expand Down Expand Up @@ -49,7 +49,7 @@ def get(self, message_id: str, locale: str | None = None, /, **kwargs: Any) -> s
return message_id
text, errors = translator.format_pattern(pattern=message.value, args=kwargs)
if errors:
raise errors[0]
raise FluentMessageError(message_id=message_id, errors=errors)
return cast(str, text)

def find_locales(self) -> dict[str, FluentBundle]:
Expand Down
12 changes: 12 additions & 0 deletions src/aiogram_i18n/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ def __init__(self, locale: str) -> None:

def __str__(self) -> str:
return self.message.format(locale=self.locale)


class FluentMessageError(AiogramI18nError):
def __init__(self, message_id: str, errors: list[Exception]) -> None:
self.message_id = message_id
self.errors = errors

def __str__(self) -> str:
error_word = "error" if len(self.errors) == 1 else "errors"
lines = [f"\n{len(self.errors)} {error_word} for key '{self.message_id}':"]
lines.extend(f" {err} (type={type(err).__name__})" for err in self.errors)
return "\n".join(lines)