mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-06 16:15:51 +00:00
Refactor examples/i18n_example.py
This commit is contained in:
parent
d360538bba
commit
a78b93c5cd
5 changed files with 37 additions and 23 deletions
|
|
@ -54,14 +54,16 @@ dp.middleware.setup(i18n)
|
|||
_ = i18n.gettext
|
||||
|
||||
|
||||
@dp.message_handler(commands=['start'])
|
||||
@dp.message_handler(commands='start')
|
||||
async def cmd_start(message: types.Message):
|
||||
# Simply use `_('message')` instead of `'message'` and never use f-strings for translatable texts.
|
||||
await message.reply(_('Hello, <b>{user}</b>!').format(user=message.from_user.full_name))
|
||||
|
||||
|
||||
@dp.message_handler(commands=['lang'])
|
||||
@dp.message_handler(commands='lang')
|
||||
async def cmd_lang(message: types.Message, locale):
|
||||
# For setting custom lang you have to modify i18n middleware, like this:
|
||||
# https://github.com/aiogram/EventsTrackerBot/blob/master/modules/base/middlewares.py
|
||||
await message.reply(_('Your current language: <i>{language}</i>').format(language=locale))
|
||||
|
||||
# If you care about pluralization, here's small handler
|
||||
|
|
@ -70,15 +72,27 @@ async def cmd_lang(message: types.Message, locale):
|
|||
# Alias for gettext method, parser will understand double underscore as plural (aka ngettext)
|
||||
__ = i18n.gettext
|
||||
|
||||
# Some pseudo numeric value
|
||||
TOTAL_LIKES = 0
|
||||
|
||||
@dp.message_handler(commands=['like'])
|
||||
# some likes manager
|
||||
LIKES_STORAGE = {'count': 0}
|
||||
|
||||
|
||||
def get_likes() -> int:
|
||||
return LIKES_STORAGE['count']
|
||||
|
||||
|
||||
def increase_likes() -> int:
|
||||
LIKES_STORAGE['count'] += 1
|
||||
return get_likes()
|
||||
#
|
||||
|
||||
|
||||
@dp.message_handler(commands='like')
|
||||
async def cmd_like(message: types.Message, locale):
|
||||
TOTAL_LIKES += 1
|
||||
likes = increase_likes()
|
||||
|
||||
# NOTE: This is comment for a translator
|
||||
await message.reply(__('Aiogram has {number} like!', 'Aiogram has {number} likes!', TOTAL_LIKES).format(number=TOTAL_LIKES))
|
||||
await message.reply(__('Aiogram has {number} like!', 'Aiogram has {number} likes!', likes).format(number=likes))
|
||||
|
||||
if __name__ == '__main__':
|
||||
executor.start_polling(dp, skip_updates=True)
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ msgid ""
|
|||
msgstr ""
|
||||
"Project-Id-Version: PROJECT VERSION\n"
|
||||
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
|
||||
"POT-Creation-Date: 2019-07-22 20:45+0300\n"
|
||||
"POT-Creation-Date: 2019-08-10 17:51+0300\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
|
|
@ -21,7 +21,6 @@ msgstr ""
|
|||
msgid "Hello, <b>{user}</b>!"
|
||||
msgstr ""
|
||||
|
||||
#: i18n_example.py:65
|
||||
#: i18n_example.py:67
|
||||
msgid "Your current language: <i>{language}</i>"
|
||||
msgstr ""
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
# Russian translations for PROJECT.
|
||||
# Copyright (C) 2018 ORGANIZATION
|
||||
# Copyright (C) 2019 ORGANIZATION
|
||||
# This file is distributed under the same license as the PROJECT project.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, 2018.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, 2019.
|
||||
#
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: PROJECT VERSION\n"
|
||||
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
|
||||
"POT-Creation-Date: 2018-06-30 03:50+0300\n"
|
||||
"PO-Revision-Date: 2018-06-30 03:43+0300\n"
|
||||
"POT-Creation-Date: 2019-08-10 17:51+0300\n"
|
||||
"PO-Revision-Date: 2019-08-10 17:52+0300\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language: ru\n"
|
||||
"Language-Team: ru <LL@li.org>\n"
|
||||
|
|
@ -17,18 +17,19 @@ msgstr ""
|
|||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=utf-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
"Generated-By: Babel 2.6.0\n"
|
||||
"Generated-By: Babel 2.7.0\n"
|
||||
|
||||
#: i18n_example.py:48
|
||||
#: i18n_example.py:60
|
||||
msgid "Hello, <b>{user}</b>!"
|
||||
msgstr "Привет, <b>{user}</b>!"
|
||||
|
||||
#: i18n_example.py:53
|
||||
#: i18n_example.py:67
|
||||
msgid "Your current language: <i>{language}</i>"
|
||||
msgstr "Твой язык: <i>{language}</i>"
|
||||
|
||||
#: i18n_example.py:95
|
||||
msgid "Aiogram has {number} like!"
|
||||
msgid_plural "Aiogram has {number} likes!"
|
||||
msgstr[0] "Aiogram имеет {number} лайк!"
|
||||
msgstr[1] "Aiogram имеет {number} лайка!"
|
||||
msgstr[2] "Aiogram имеет {number} лайков!"
|
||||
msgstr[2] "Aiogram имеет {number} лайков!"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue