Welcome to aiogram-i18n’s documentation!

https://img.shields.io/pypi/v/aiogram-i18n.svg https://img.shields.io/pypi/pyversions/aiogram-i18n.svg https://img.shields.io/pypi/l/aiogram-i18n.svg https://img.shields.io/pypi/dm/aiogram-i18n.svg Documentation Status

aiogram_i18n, an unrivalled middleware for Telegram bot internationalization, breathes life into bots, enabling diverse interactions in multiple languages, while adeptly managing user context, paving the way for a truly engaging and immersive user experience irrespective of language preference, and providing robust support for both Fluent and GNU gettext localization systems, thereby offering flexibility in translation file format selection, streamlining the translation process, and making the creation of multilingual bots an achievable goal for developers.

Installation

To install aiogram-i18n without any backends:

pip install aiogram-i18n

If you need help with what backend to choose, read Backends.

To use FluentCompileCore

To use fluent-compiler backend (aiogram_i18n.cores.fluent_compile_core.FluentCompileCore) install it:

pip install aiogram-i18n[compiler]

To use FluentRuntimeCore

To use Fluent.runtime backend (aiogram_i18n.cores.fluent_runtime_core.FluentRuntimeCore) install it:

pip install aiogram-i18n[runtime]

To use GNU gettext

To use gettext backend (aiogram_i18n.cores.gnu_text_core.py.GettextCore) install it:

pip install aiogram-i18n[gettext]

Usage example

 1import asyncio
 2from contextlib import suppress
 3from logging import INFO, basicConfig
 4from typing import Any
 5
 6from aiogram import Bot, Dispatcher, Router
 7from aiogram.client.default import DefaultBotProperties
 8from aiogram.enums import ParseMode
 9from aiogram.filters import CommandStart
10from aiogram.types import Message
11
12from aiogram_i18n import I18nContext, I18nMiddleware, LazyProxy
13from aiogram_i18n.cores.fluent_runtime_core import FluentRuntimeCore
14from aiogram_i18n.lazy.filter import LazyFilter
15from aiogram_i18n.types import (
16    KeyboardButton,
17    ReplyKeyboardMarkup,
18)  # you should import mutable objects from here if you want to use LazyProxy in them
19
20router = Router(name=__name__)
21rkb = ReplyKeyboardMarkup(
22    keyboard=[[KeyboardButton(text=LazyProxy("help"))]], resize_keyboard=True  # or L.help()
23)
24
25
26@router.message(CommandStart())
27async def cmd_start(message: Message, i18n: I18nContext) -> Any:
28    name = message.from_user.mention_html()
29    return message.reply(
30        text=i18n.get("hello", user=name), reply_markup=rkb  # or i18n.hello(user=name)
31    )
32
33
34@router.message(LazyFilter("help"))
35async def cmd_help(message: Message) -> Any:
36    return message.reply(text="-- " + message.text + " --")
37
38
39async def main() -> None:
40    basicConfig(level=INFO)
41    bot = Bot("42:ABC", default=DefaultBotProperties(parse_mode=ParseMode.HTML))
42    i18n_middleware = I18nMiddleware(core=FluentRuntimeCore(path="locales/{locale}/LC_MESSAGES"))
43
44    dp = Dispatcher()
45    dp.include_router(router)
46    i18n_middleware.setup(dispatcher=dp)
47
48    await dp.start_polling(bot)
49
50
51if __name__ == "__main__":
52    with suppress(KeyboardInterrupt):
53        asyncio.run(main())

Indices and tables