Welcome to aiogram-i18n’s documentation!#

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

Indices and tables#