Welcome to aiogram-i18n’s documentation!#
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#
- Backends
- Fluent runtime
- fluent.runtime
- Fluent compile
- GNU gettext
- Pros of using GNU gettext
- Cons of using GNU gettext
- Install GNU gettext
- Fluent_compile
FluentCompileCore
FluentCompileCore.available_locales
FluentCompileCore.default_locale
FluentCompileCore.find_locales()
FluentCompileCore.get()
FluentCompileCore.get_locale()
FluentCompileCore.get_translator()
FluentCompileCore.locales
FluentCompileCore.locales_map
FluentCompileCore.nget()
FluentCompileCore.shutdown()
FluentCompileCore.startup()
- Fluent.runtime
FluentRuntimeCore
FluentRuntimeCore.available_locales
FluentRuntimeCore.default_locale
FluentRuntimeCore.find_locales()
FluentRuntimeCore.get()
FluentRuntimeCore.get_locale()
FluentRuntimeCore.get_translator()
FluentRuntimeCore.locales
FluentRuntimeCore.locales_map
FluentRuntimeCore.nget()
FluentRuntimeCore.shutdown()
FluentRuntimeCore.startup()
- GNU Gettext
- Writing own backends
- Fluent_compile
- Poem