ugettext and ugettext_lazy functions not recognized by makemessages in Python Django

Django command utility makemessages internally calls xgettext program like this:

cmd = (
    'xgettext -d %s -L Python %s %s --keyword=gettext_noop '
    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
    '--keyword=ugettext_noop --keyword=ugettext_lazy '
    '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
    '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
    '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
    '--add-comments=Translators -o - "%s"' %
    (domain, wrap, location, work_file))

(source can be found here). So, some keywords are predefined by the xgettext utility (check reference for --keyword):

  • for python - gettext, ugettext, dgettext, ngettext, ungettext, dngettext, _

and some are added by django utility:

  • gettext_lazy , ngettext_lazy , ugettext_noop , ugettext_lazy , ungettext_lazy , pgettext , npgettext , pgettext_lazy , npgettext_lazy

Keyword trans is not in any of these keyword sets, so you should not use it for marking texts for translations.


Unexpected ugettext aliases can be handled by overriding the makemessages command, such as in:

from django.core.management.commands import makemessages

class Command(makemessages.Command):
    """
    Extends the makemessages command to look for additional aliases.
    """
    xgettext_options = makemessages.Command.xgettext_options + ['--keyword=_lazy']

See https://docs.djangoproject.com/en/1.8/topics/i18n/translation/#customizing-the-makemessages-command