Skip to main content

small translation tools for aiogram

Project description

aiogram_i18n

Installation: pip install aiogram_i18n

To use FluentCompileCore: pip install fluent_compiler

To use 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 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", 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())

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.4.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

aiogram_i18n-1.4-py3-none-any.whl (37.0 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_i18n-1.4.tar.gz.

File metadata

  • Download URL: aiogram_i18n-1.4.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for aiogram_i18n-1.4.tar.gz
Algorithm Hash digest
SHA256 6e41304a166fe3bccb74f98b00dc4e85fac0cee815ae7f2c85f539e37e65818b
MD5 ae8c718b3b078c98260f4a1502ba80fa
BLAKE2b-256 edb497191bf90bcd5ee37990a233b0d3d611ea5037dbf768a28c0d196cb32f4f

See more details on using hashes here.

File details

Details for the file aiogram_i18n-1.4-py3-none-any.whl.

File metadata

  • Download URL: aiogram_i18n-1.4-py3-none-any.whl
  • Upload date:
  • Size: 37.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for aiogram_i18n-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a1f838cfd6fa9a459d5f4514e9693e4fe75d27a0138686d92880faff11447007
MD5 984bf52a10d4c93d9162741508fb4117
BLAKE2b-256 0fbaf978b917b911bc610b8266978b3ab460b882031fc9665e9daff4498650bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page