Giter Site home page Giter Site logo

i18n about aiogram_dialog HOT 3 CLOSED

tishka17 avatar tishka17 commented on June 12, 2024
i18n

from aiogram_dialog.

Comments (3)

IrSent avatar IrSent commented on June 12, 2024 1

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.

IrSent avatar IrSent commented on June 12, 2024

Hi @Tishka17 , are there any thoughts on how to make i18n for this framework?

from aiogram_dialog.

Tishka17 avatar Tishka17 commented on June 12, 2024

Added example, not builin support is currently planned due to difficulties with API unification.

from aiogram_dialog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.