https://github.com/aiogram/i18n
Translation tool for aiogram
https://github.com/aiogram/i18n
Last synced: 4 months ago
JSON representation
Translation tool for aiogram
- Host: GitHub
- URL: https://github.com/aiogram/i18n
- Owner: aiogram
- Created: 2023-06-28T23:24:01.000Z (almost 3 years ago)
- Default Branch: dev
- Last Pushed: 2026-03-04T07:04:51.000Z (4 months ago)
- Last Synced: 2026-03-04T12:52:11.113Z (4 months ago)
- Language: Python
- Homepage:
- Size: 321 KB
- Stars: 44
- Watchers: 4
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# aiogram_i18n
Installation:
```pip install aiogram_i18n```
To use FluentCompileCore:
```pip install fluent_compiler```
To use FluentRuntimeCore:
```pip install fluent.runtime```
```python
import asyncio
from contextlib import suppress
from logging import basicConfig, INFO
from typing import Any
from aiogram import Router, Dispatcher, Bot
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import Message
from aiogram_i18n import I18nContext, LazyProxy, I18nMiddleware, LazyFilter
from aiogram_i18n.cores.fluent_runtime_core import FluentRuntimeCore
from aiogram_i18n.types import (
ReplyKeyboardMarkup, KeyboardButton
# you should import mutable objects from here if you want to use LazyProxy in them
)
router = Router(name=__name__)
rkb = ReplyKeyboardMarkup(
keyboard=[
[KeyboardButton(text=LazyProxy("help"))] # or L.help()
], resize_keyboard=True
)
@router.message(CommandStart())
async def cmd_start(message: Message, i18n: I18nContext) -> Any:
name = message.from_user.mention_html()
return message.reply(
text=i18n.get("hello", user=name), # or i18n.hello(user=name)
reply_markup=rkb
)
@router.message(LazyFilter("help")) # or LazyProxy("help") or F.text == LazyProxy("help")
async def cmd_help(message: Message) -> Any:
return message.reply(text="-- " + message.text + " --")
async def main() -> None:
basicConfig(level=INFO)
bot = Bot("42:ABC", default=DefaultBotProperties(parse_mode=ParseMode.HTML))
i18n_middleware = I18nMiddleware(
core=FluentRuntimeCore(
path="locales/{locale}/LC_MESSAGES"
)
)
dp = Dispatcher()
dp.include_router(router)
i18n_middleware.setup(dispatcher=dp)
await dp.start_polling(bot)
if __name__ == "__main__":
with suppress(KeyboardInterrupt):
asyncio.run(main())
```