Comments (3)
Sharing on how I'm doing it together with django:
i18n.py
from aiogram.contrib.middlewares.i18n import I18nMiddleware
from django.conf import settings
from django.utils.translation.trans_real import translation
class DjangoI18nMiddleware(I18nMiddleware):
domain = "django"
def __init__(self, *args, **kwargs):
super(I18nMiddleware, self).__init__(*args, **kwargs)
self.locales = self.find_locales()
def find_locales(self):
"""
Load all compiled locales from path
:return: dict with locales
"""
translations = {}
for lang, _ in dict(settings.LANGUAGES).items():
translations[lang] = translation(lang)
return translations
instantiating middleware in the separate translation.py file:
gettext = DjangoI18nMiddleware()
linking middleware into dispatcher
from .translation import gettext
...
dp.middleware.setup(gettext)
...
use Format instead of Const to make things happen.
usage example in menu.py:
from .translation import gettext
...
async def get_data(dialog_manager: DialogManager, **kwargs):
dialog_manager.current_context().dialog_data = {
**dialog_manager.current_context().start_data,
**dialog_manager.current_context().dialog_data,
}
return dialog_manager.current_context().dialog_data
...
main_menu_window = Window(
Format("{_[Example text]}"),
Button(
Format("{_[Click me]}"),
id="click",
on_click=to_somewhere,
),
state=MenuSG.main_menu,
getter=get_data,
)
...
menu_dialog = Dialog(main_menu_window)
...
@dp.message_handler(commands=["start", "menu"], state="*")
async def menu(msg: Message, dialog_manager: DialogManager):
user = dict(msg.from_user)
start_data = {"_": {k: v for k, v in gettext.locales[tg_user.language]._catalog.items()}}
await dialog_manager.start(MenuSG.main_menu, start_data, mode=StartMode.RESET_STACK)
...
# for babel / django makemessages
trans = [
_("Example text"),
_("Click me"),
]
from aiogram_dialog.
Hi @Tishka17 , are there any thoughts on how to make i18n for this framework?
from aiogram_dialog.
Added example, not builin support is currently planned due to difficulties with API unification.
from aiogram_dialog.
Related Issues (20)
- ListGroup id HOT 1
- Window events
- ManagerRadio unchecked all HOT 1
- How to use old windows after restarting HOT 4
- show_mode in SwitchTo Widged
- Implementing Multiple Dynamic Counter Widgets with Text Parameters in aiogram-dialog HOT 1
- ShowMode in manager.start() doesn't work as expected by some users HOT 2
- Updating the examples.
- Launching a dialog with the transmission of dynamic data for the status of the checkbox buttons HOT 1
- Start calendar widget with scope Year HOT 1
- Calendar bug while selected date less than 1970 HOT 2
- How i can store data in state using FSMContext HOT 4
- Using payments HOT 1
- Coverage report falied on Jinja widgets HOT 1
- Handling InaccessibleMessage in get_media_id Function
- Best solution for creating different count of buttons
- LinkPreview widget HOT 1
- Reply for button on message HOT 3
- Multiselect и Select с предустановленным состоянием HOT 1
- New rendering protocol
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiogram_dialog.