# 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, 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())
```
Raw data
{
"_id": null,
"home_page": null,
"name": "aiogram-i18n",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "aiogram, api, asyncio, bot, fluent, framework, gnutext, i18n, telegram, wrapper",
"author": "RootShinobi",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/ed/b4/97191bf90bcd5ee37990a233b0d3d611ea5037dbf768a28c0d196cb32f4f/aiogram_i18n-1.4.tar.gz",
"platform": null,
"description": "# aiogram_i18n\n\nInstallation:\n```pip install aiogram_i18n```\n\nTo use FluentCompileCore:\n```pip install fluent_compiler```\n\nTo use FluentRuntimeCore:\n```pip install fluent.runtime```\n\n```python\nimport asyncio\nfrom contextlib import suppress\nfrom logging import basicConfig, INFO\nfrom typing import Any\n\nfrom aiogram import Router, Dispatcher, F, Bot\nfrom aiogram.enums import ParseMode\nfrom aiogram.filters import CommandStart\nfrom aiogram.types import Message\n\nfrom aiogram_i18n import I18nContext, LazyProxy, I18nMiddleware, LazyFilter\nfrom aiogram_i18n.cores.fluent_runtime_core import FluentRuntimeCore\nfrom aiogram_i18n.types import (\n ReplyKeyboardMarkup, KeyboardButton\n # you should import mutable objects from here if you want to use LazyProxy in them\n)\n\nrouter = Router(name=__name__)\nrkb = ReplyKeyboardMarkup(\n keyboard=[\n [KeyboardButton(text=LazyProxy(\"help\"))] # or L.help()\n ], resize_keyboard=True\n)\n\n\n@router.message(CommandStart())\nasync def cmd_start(message: Message, i18n: I18nContext) -> Any:\n name = message.from_user.mention_html()\n return message.reply(\n text=i18n.get(\"hello\", user=name), # or i18n.hello(user=name)\n reply_markup=rkb\n )\n\n\n@router.message(LazyFilter(\"help\")) # or LazyProxy(\"help\") or F.text == LazyProxy(\"help\")\nasync def cmd_help(message: Message) -> Any:\n return message.reply(text=\"-- \" + message.text + \" --\")\n\n\nasync def main() -> None:\n basicConfig(level=INFO)\n bot = Bot(\"42:ABC\", parse_mode=ParseMode.HTML)\n i18n_middleware = I18nMiddleware(\n core=FluentRuntimeCore(\n path=\"locales/{locale}/LC_MESSAGES\"\n )\n )\n\n dp = Dispatcher()\n dp.include_router(router)\n i18n_middleware.setup(dispatcher=dp)\n\n await dp.start_polling(bot)\n\n\nif __name__ == \"__main__\":\n with suppress(KeyboardInterrupt):\n asyncio.run(main())\n```\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "small translation tools for aiogram",
"version": "1.4",
"project_urls": {
"Repository": "https://github.com/aiogram/aiogram_i18n"
},
"split_keywords": [
"aiogram",
" api",
" asyncio",
" bot",
" fluent",
" framework",
" gnutext",
" i18n",
" telegram",
" wrapper"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0fbaf978b917b911bc610b8266978b3ab460b882031fc9665e9daff4498650bd",
"md5": "984bf52a10d4c93d9162741508fb4117",
"sha256": "a1f838cfd6fa9a459d5f4514e9693e4fe75d27a0138686d92880faff11447007"
},
"downloads": -1,
"filename": "aiogram_i18n-1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "984bf52a10d4c93d9162741508fb4117",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 37045,
"upload_time": "2024-06-04T12:37:20",
"upload_time_iso_8601": "2024-06-04T12:37:20.008909Z",
"url": "https://files.pythonhosted.org/packages/0f/ba/f978b917b911bc610b8266978b3ab460b882031fc9665e9daff4498650bd/aiogram_i18n-1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "edb497191bf90bcd5ee37990a233b0d3d611ea5037dbf768a28c0d196cb32f4f",
"md5": "ae8c718b3b078c98260f4a1502ba80fa",
"sha256": "6e41304a166fe3bccb74f98b00dc4e85fac0cee815ae7f2c85f539e37e65818b"
},
"downloads": -1,
"filename": "aiogram_i18n-1.4.tar.gz",
"has_sig": false,
"md5_digest": "ae8c718b3b078c98260f4a1502ba80fa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 33451,
"upload_time": "2024-06-04T12:37:21",
"upload_time_iso_8601": "2024-06-04T12:37:21.771166Z",
"url": "https://files.pythonhosted.org/packages/ed/b4/97191bf90bcd5ee37990a233b0d3d611ea5037dbf768a28c0d196cb32f4f/aiogram_i18n-1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-04 12:37:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "aiogram",
"github_project": "aiogram_i18n",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "aiogram-i18n"
}