Simple aiogram translation plugin built using pydantic.
Project description
About 📘
The Aiogram Translation Plugin is a convenient and powerful tool for integrating multilingual support into Aiogram-based Telegram bots. It enables seamless translation and language handling, making your bot accessible to a wider, global audience.
Installation 📥
python -m pip install -U aiogram-translation
Usage 🛠️
To use the Aiogram Translation Plugin in your bot, import the necessary classes from aiogram_translation
. Set up your languages, default language, and register the translator with your dispatcher. Here's a basic example:
main.py
from aiogram import Dispatcher, Bot, F
from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
from aiogram_translation import Translator
from translations import *
from os import getenv
bot = Bot(getenv("TELEGRAM_TOKEN"))
dispatcher = Dispatcher()
translator = Translator()
translator.include([
English(),
Russian(),
Ukrainian()
])
translator.set_default('ru')
translator.register(dispatcher)
@dispatcher.message()
async def on_message(message: Message, language: BaseTranslation):
kb = InlineKeyboardMarkup(resize_keyboard=True, inline_keyboard=[[InlineKeyboardButton(text=language.start_button,
callback_data="hoooray")]])
await message.reply(language.start_message, reply_markup=kb)
@dispatcher.callback_query(F.data == "hoooray")
async def on_hooray(query: CallbackQuery, language: BaseTranslation):
await query.answer(language.start_button_alert, show_alert=True)
dispatcher.run_polling(bot)
translations.py
from aiogram_translation.models import BaseTranslationBuilder
class BaseTranslation(BaseTranslationBuilder):
start_message: str
start_button: str
start_button_alert: str
link_lang_message: str
class English(BaseTranslation):
key = "en"
name = "English"
start_message = "👋 Hi, I'm bot!"
start_button = "❤️ Click me!"
start_button_alert = "🎉 Hooray!"
class Russian(BaseTranslation):
key = "ru"
name = "Русский (Russian)"
start_message = "👋 Привет, я бот"
start_button = "❤️ Нажми на меня"
start_button_alert = "🎉 Ура!"
class Ukrainian(BaseTranslation):
key = "uk"
name = "Український (Ukrainian)"
start_message = "👋 Привіт, я бот"
start_button = "❤️ Натисни на мене"
start_button_alert = "🎉 Ура!"
Troubleshooting 🚨
If you encounter issues or have queries, feel free to check our Issues section on GitHub.
Contribution 🤝
Contributions are welcome. Please fork the repository, make your changes, and submit a pull request.
License 📜
This project is licensed under the MIT License - see the LICENSE file for details.
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
Built Distribution
File details
Details for the file aiogram_translation-2.0.1.tar.gz
.
File metadata
- Download URL: aiogram_translation-2.0.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac84d99ad3f4c0f12d55ead058d6130b591919337f93dc3a6c91410b20c7c3bf |
|
MD5 | 41e6392cceec0bd46cac078f3fd071de |
|
BLAKE2b-256 | 537c3469cebb42f8149a6bbcd4b479ccd0eb6ddba8c96d25aa7a2368e51e0b12 |
File details
Details for the file aiogram_translation-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: aiogram_translation-2.0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1edd0b941ddd79d902dad83fb03dff1782f881ea86fa24f458ce7fd73cf13b3b |
|
MD5 | 6d92775d3debc71cb0570598b96932bf |
|
BLAKE2b-256 | 1674fdf90f807f594e4a9130e0378276b6f9b8a6ce2fafaaf0a48ad6df95c359 |