small translation tools for aiogram
Project description
aiogram_i18n
FluentCompileCore:
pip install fluent_compiler
FluentRuntimeCore:
pip install fluent.runtime
import asyncio
from contextlib import suppress
from logging import basicConfig, INFO
from typing import Any
from aiogram import Router, Dispatcher, F, Bot
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import ReplyKeyboardMarkup, Message
from aiogram_i18n import I18nContext, LazyProxy, I18nMiddleware
from aiogram_i18n.cores.fluent_runtime_core import FluentRuntimeCore
from aiogram_i18n.utils.keyboard import KeyboardButton # you should import the keyboard from here if you want to use LazyProxy
router = Router(name=__name__)
rkb = ReplyKeyboardMarkup(
keyboard=[
[KeyboardButton(text=LazyProxy(key="help"))]
], resize_keyboard=True
)
@router.message(CommandStart())
async def cmd_start(message: Message, i18n: I18nContext) -> Any:
return message.reply(
text=i18n.get("hello", user=message.from_user.full_name),
reply_markup=rkb
)
@router.message(F.text == LazyProxy(key="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", parse_mode=ParseMode.HTML)
i18n = I18nMiddleware(
core=FluentRuntimeCore(
path="locales/{locale}/LC_MESSAGES"
)
)
dp = Dispatcher()
dp.include_router(router)
i18n.setup(dispatcher=dp)
await dp.start_polling(bot)
if __name__ == "__main__":
with suppress(KeyboardInterrupt):
asyncio.run(main())
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiogram_i18n-1.0.tar.gz
(7.7 kB
view hashes)
Built Distribution
aiogram_i18n-1.0-py3-none-any.whl
(12.2 kB
view hashes)
Close
Hashes for aiogram_i18n-1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d96f580a23cdd3c59c3c9b51a322710ad3511131d1511f2aba30a39c766da9a |
|
MD5 | d7f63c8ef93ce250450500321a1c7e1d |
|
BLAKE2b-256 | 23b6aef90b8db479582caa3e12380b9f3b128c5b4c7776f99a42f2e43eb681be |