OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Comparing message text to fickle function result in Aiogram message handler

  • Thread starter Thread starter Mipsirint
  • Start date Start date
M

Mipsirint

Guest
In my Aiogram router code I have several handlers that check the user's text against the buttons text translated by gettext module:

Code:
@router.message(F.text == gettext("Settings"))
async def view_settings(message: Message):
    ...

In the beginning of the code I set up the gettext function default value:

Code:
import gettext as gt
gettext = gt.translation("ru", "translations", languages=["ru"]).gettext

which returns the function for translating buttons text into Russian.

Every handler works fine until user set up their language to another language like English or Spanish:

Code:
gettext = gt.translation("en", "translations", languages=["en"]).gettext

The user's language is not Russian anymore but the handlers still compare user's text to buttons text that was translated into the first initiated language, the Russian.

Is there any way to recalculate gettext function value in the handlers every time when Aiogram dispatcher decides a handler to call? I'll say in advance, don't suggest switching to inline-buttons.

P.S. I would be glad to make a single @router.message(F.text) handler where I would check the text alternately via if-else, but because I need to use a FSM in the handlers arguments, this option is not suitable.

The full code that has the issue (some unimportant things were removed to boost the readability):

Code:
import gettext as gt
from aiogram import F, Router
from aiogram.types import Message
from aiogram.utils.keyboard import ReplyKeyboardBuilder

gettext = gt.translation("ru", "translations", languages=["ru"]).gettext

router = Router()


@router.message(F.text == "English")
async def change_to_english_language(message: Message):
    global gettext
    gettext = gt.translation("en", "translations", languages=["en"]).gettext
    await message.answer(gettext("Now your language is English"))


@router.message(F.text == gettext("Settings"))
async def view_settings(message: Message):
    builder = ReplyKeyboardBuilder()
    builder.button(text=gettext("Language"))
    builder.button(text=gettext("Preferred name"))
    await message.answer(gettext("What do you want to set up?"),
                         reply_markup=builder.as_markup(resize_keyboard=True))
<p>In my Aiogram router code I have several handlers that check the user's text against the buttons text translated by <code>gettext</code> module:</p>
<pre class="lang-py prettyprint-override"><code>@router.message(F.text == gettext("Settings"))
async def view_settings(message: Message):
...
</code></pre>
<p>In the beginning of the code I set up the <code>gettext</code> function default value:</p>
<pre class="lang-py prettyprint-override"><code>import gettext as gt
gettext = gt.translation("ru", "translations", languages=["ru"]).gettext
</code></pre>
<p>which returns the function for translating buttons text into Russian.</p>
<p>Every handler works fine until user set up their language to another language like English or Spanish:</p>
<pre class="lang-py prettyprint-override"><code>gettext = gt.translation("en", "translations", languages=["en"]).gettext
</code></pre>
<p>The user's language is not Russian anymore but the handlers still compare user's text to buttons text that was translated into the first initiated language, the Russian.</p>
<p>Is there any way to recalculate <code>gettext</code> function value in the handlers every time when Aiogram dispatcher decides a handler to call? I'll say in advance, don't suggest switching to inline-buttons.</p>
<p>P.S. I would be glad to make a single <code>@router.message(F.text)</code> handler where I would check the text alternately via if-else, but because I need to use a FSM in the handlers arguments, this option is not suitable.</p>
<p>The full code that has the issue (some unimportant things were removed to boost the readability):</p>
<pre class="lang-py prettyprint-override"><code>import gettext as gt
from aiogram import F, Router
from aiogram.types import Message
from aiogram.utils.keyboard import ReplyKeyboardBuilder

gettext = gt.translation("ru", "translations", languages=["ru"]).gettext

router = Router()


@router.message(F.text == "English")
async def change_to_english_language(message: Message):
global gettext
gettext = gt.translation("en", "translations", languages=["en"]).gettext
await message.answer(gettext("Now your language is English"))


@router.message(F.text == gettext("Settings"))
async def view_settings(message: Message):
builder = ReplyKeyboardBuilder()
builder.button(text=gettext("Language"))
builder.button(text=gettext("Preferred name"))
await message.answer(gettext("What do you want to set up?"),
reply_markup=builder.as_markup(resize_keyboard=True))
</code></pre>
 
Top