Search code examples
djangotranslationgettext

django translation ngettext not working with gettext in the same file


this is my setup to generate translations for both singular and plurar text

from django.utils.translations import ngettext as _
from django.utils.translations import gettext

num = 3
my_plural_string = _("{num} apple", "{num} apples", num).format(num=num)
my_single_string = gettext("this is a text")


When using ngettext and gettext in the same file the generated .po file doesn't include the msgid_plural attribute for the first string

#: .\test_app\test_translation.py:10
msgid "{num} apple"
msgstr ""

#: .\test_app\test_translation.py:11
msgid "this is a text"
msgstr ""

I don't understand what is the problem in this code? Do I need to manually add the msgid_plural to each plural form of the strings ?


Solution

  • You can't alias ngettext as _. Only functions with single argument (gettext or gettext_lazy) can be aliased as _ as stated in documentation. The reason is how xgettext (GNU gettext tool) works.