diff --git a/examples/i18n_example.py b/examples/i18n_example.py index 6469ed5b..bf23c8d1 100644 --- a/examples/i18n_example.py +++ b/examples/i18n_example.py @@ -3,6 +3,19 @@ Internalize your bot Step 1: extract texts # pybabel extract i18n_example.py -o locales/mybot.pot + + Some useful options: + - Extract texts with pluralization support + # -k __:1,2 + - Add comments for translators, you can use another tag if you want (TR) + # --add-comments=NOTE + - Disable comments with string location in code + # --no-location + - Set project name + # --project=MySuperBot + - Set version + # --version=2.2 + Step 2: create *.po files. For e.g. create en, ru, uk locales. # echo {en,ru,uk} | xargs -n1 pybabel init -i locales/mybot.pot -d locales -D mybot -l Step 3: translate texts @@ -51,6 +64,21 @@ async def cmd_start(message: types.Message): async def cmd_lang(message: types.Message, locale): await message.reply(_('Your current language: {language}').format(language=locale)) +# If you care about pluralization, here's small handler +# And also, there's and example of comments for translators. Most translation tools support them. + +# 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']) +async def cmd_like(message: types.Message, locale): + TOTAL_LIKES += 1 + + # NOTE: This is comment for a translator + await message.reply(__('Aiogram has {number} like!', 'Aiogram has {number} likes!', TOTAL_LIKES).format(number=TOTAL_LIKES)) if __name__ == '__main__': executor.start_polling(dp, skip_updates=True) diff --git a/examples/locales/mybot.pot b/examples/locales/mybot.pot index 988ed463..b0736569 100644 --- a/examples/locales/mybot.pot +++ b/examples/locales/mybot.pot @@ -25,3 +25,7 @@ msgstr "" msgid "Your current language: {language}" msgstr "" +msgid "Aiogram has {number} like!" +msgid_plural "Aiogram has {number} likes!" +msgstr[0] "" +msgstr[1] "" diff --git a/examples/locales/ru/LC_MESSAGES/mybot.po b/examples/locales/ru/LC_MESSAGES/mybot.po index 73876f30..8180af42 100644 --- a/examples/locales/ru/LC_MESSAGES/mybot.po +++ b/examples/locales/ru/LC_MESSAGES/mybot.po @@ -27,3 +27,8 @@ msgstr "Привет, {user}!" msgid "Your current language: {language}" msgstr "Твой язык: {language}" +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