xgettext and comments

Hi!

Today I ran python scripts/manage_translations.py update_catalogs from the root of the django project, but it looks like it removed some comments, I’m slowly digging and found that there’s something to do with xgettext and line breaks, maybe the specific version of xgettext I’m using? I’m using:

$ xgettext --version
xgettext (GNU gettext-tools) 0.22.5

from a Debian testing.

I’m working in humanize.py and if I keep the original lines like:

    past_substrings = {
        # Translators: 'naturaltime-past' strings will be included in '%(delta)s ago'
        "year": npgettext_lazy(
            "naturaltime-past", "%(num)d year", "%(num)d years", "num"
        ),

then the Translators comment is not picked up by my xgettext (while it was in the po file, so it was previously picked-up by someone else xgettext) (I copied the xgettext call from an strace):

$ xgettext "-d" "django" "--language=Python" "--keyword=gettext_noop" "--keyword=gettext_lazy" "--keyword=ngettext_lazy:1,2" "--keyword=pgettext:1c,2" "--keyword=npgettext:1c,2,3" "--keyword=pgettext_lazy:1c,2" "--keyword=npgettext_lazy:1c,2,3" "--output=-" django/contrib/humanize/templatetags/humanize.py "--from-code=UTF-8" "--add-comments=Translators" | grep -c Translators
18

But if I change the line to be:

    past_substrings = {
        # Translators: 'naturaltime-past' strings will be included in '%(delta)s ago'
        "year": npgettext_lazy("naturaltime-past", "%(num)d year", "%(num)d years", "num"),

then the comment is picked up (see the 18 comments becoming 19 comments):

$ xgettext "-d" "django" "--language=Python" "--keyword=gettext_noop" "--keyword=gettext_lazy" "--keyword=ngettext_lazy:1,2" "--keyword=pgettext:1c,2" "--keyword=npgettext:1c,2,3" "--keyword=pgettext_lazy:1c,2" "--keyword=npgettext_lazy:1c,2,3" "--output=-" django/contrib/humanize/templatetags/humanize.py "--from-code=UTF-8" "--add-comments=Translators" | grep -c Translators
19
1 Like

I’m getting the same behavior with gettext 0.21, so it’s not a very recent change.

I think this is an issue that should be reported as a bug in the tracker. The comments should be moved a line below so they are extracted. See e.g. the position of the translator comment in django/db/models/fields/__init__.py.

You’re right, this is even documented as so in https://www.gnu.org/software/gettext/manual/html_node/xgettext-Invocation.html:

/* This is the first comment.  */
gettext ("foo");

/* This is the second comment: not extracted  */
gettext (
  "bar");

gettext (
  /* This is the third comment.  */
  "baz");

/* This is the fourth comment.  */

So it’s probably broken since 9c19aff7c7561e3a82978a272ecdaad40dda5c00.

Oh, my friend black :grin:

Are you OK with reporting the bug on code.djangoproject.com?

Yes, #36102 (Some Translators comments are dissapearing from the po files) – Django

1 Like