From a78b93c5cdeb9918893bb05247b484112ef35037 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Sat, 10 Aug 2019 17:55:39 +0300 Subject: [PATCH] Refactor examples/i18n_example.py --- aiogram/contrib/middlewares/i18n.py | 6 ++--- aiogram/types/user.py | 4 +++- examples/i18n_example.py | 28 ++++++++++++++++++------ examples/locales/mybot.pot | 5 ++--- examples/locales/ru/LC_MESSAGES/mybot.po | 17 +++++++------- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/aiogram/contrib/middlewares/i18n.py b/aiogram/contrib/middlewares/i18n.py index 8373f3d6..0bb10680 100644 --- a/aiogram/contrib/middlewares/i18n.py +++ b/aiogram/contrib/middlewares/i18n.py @@ -97,15 +97,13 @@ class I18nMiddleware(BaseMiddleware): if locale not in self.locales: if n is 1: return singular - else: - return plural + return plural translator = self.locales[locale] if plural is None: return translator.gettext(singular) - else: - return translator.ngettext(singular, plural, n) + return translator.ngettext(singular, plural, n) def lazy_gettext(self, singular, plural=None, n=1, locale=None, enable_cache=True) -> LazyProxy: """ diff --git a/aiogram/types/user.py b/aiogram/types/user.py index 441c275f..27ee27e0 100644 --- a/aiogram/types/user.py +++ b/aiogram/types/user.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import Optional + import babel from . import base @@ -45,7 +47,7 @@ class User(base.TelegramObject): return self.full_name @property - def locale(self) -> babel.core.Locale or None: + def locale(self) -> Optional[babel.core.Locale]: """ Get user's locale diff --git a/examples/i18n_example.py b/examples/i18n_example.py index bf23c8d1..f69482af 100644 --- a/examples/i18n_example.py +++ b/examples/i18n_example.py @@ -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, {user}!').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: {language}').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) diff --git a/examples/locales/mybot.pot b/examples/locales/mybot.pot index e2cf1014..62b2d425 100644 --- a/examples/locales/mybot.pot +++ b/examples/locales/mybot.pot @@ -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 \n" "Language-Team: LANGUAGE \n" @@ -21,7 +21,6 @@ msgstr "" msgid "Hello, {user}!" msgstr "" -#: i18n_example.py:65 +#: i18n_example.py:67 msgid "Your current language: {language}" msgstr "" - diff --git a/examples/locales/ru/LC_MESSAGES/mybot.po b/examples/locales/ru/LC_MESSAGES/mybot.po index 8180af42..9064bc0e 100644 --- a/examples/locales/ru/LC_MESSAGES/mybot.po +++ b/examples/locales/ru/LC_MESSAGES/mybot.po @@ -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 , 2018. +# FIRST AUTHOR , 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 \n" "Language: ru\n" "Language-Team: ru \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, {user}!" msgstr "Привет, {user}!" -#: i18n_example.py:53 +#: i18n_example.py:67 msgid "Your current language: {language}" msgstr "Твой язык: {language}" +#: 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} лайков!" \ No newline at end of file +msgstr[2] "Aiogram имеет {number} лайков!"