Opened 19 months ago

Closed 19 months ago

Last modified 19 months ago

#34630 closed Bug (worksforme)

Neither `pgettext` nor `pgettext_lazy` return contextual markers in PO files

Reported by: Kamil Paduszyński Owned by: nobody
Component: Internationalization Version: 4.2
Severity: Normal Keywords: tr
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have noticed the problem when moving from 4.1.9 to 4.2.1.

Contextual marker is returned as the message ID, whereas the actual message is ignored. For example:

  pgettext_lazy("month name", "May")

gives the following in the respective PO file:

msgid "month name"
msgstr ""

Change History (10)

comment:1 by Mariusz Felisiak, 19 months ago

Resolution: worksforme
Status: newclosed

Thanks for the report, however it works for me, see e.g. MONTHS_AP and their translations. Maybe it's an issue in gettext, I checked with gettext version 0.21.

in reply to:  1 ; comment:2 by Kamil Paduszyński, 19 months ago

Replying to Mariusz Felisiak:

Thanks for rapid review and answer!

To my surprise, it started to work on my setup (using the same version of gettext, 0.21).

But... When I was posting the ticket, pgettext_lazy was aliased it with _ (like this is commonly done with gettext_lazy). If I do not use the alias, everything works normally... So, it looks like the way pgettext_lazy works depends on the way it's imported. Is that even possible?

Thanks for the report, however it works for me, see e.g. MONTHS_AP and their translations. Maybe it's an issue in gettext, I checked with gettext version 0.21.

in reply to:  2 ; comment:3 by Mariusz Felisiak, 19 months ago

Replying to Kamil Paduszyński:

But... When I was posting the ticket, pgettext_lazy was aliased it with _ (like this is commonly done with gettext_lazy). If I do not use the alias, everything works normally... So, it looks like the way pgettext_lazy works depends on the way it's imported. Is that even possible?

I don't think so. This can be an issue in your code. You can try to prepare a minimum, sample project that reproduces your issue and ask folks on one of support channels for help in debugging.

in reply to:  3 comment:4 by Kamil Paduszyński, 19 months ago

If you'll find a minute, please take a quick look:

https://github.com/paduszykam/django-pgettext_lazy-issue

Dzięki!

Replying to Mariusz Felisiak:

Replying to Kamil Paduszyński:

But... When I was posting the ticket, pgettext_lazy was aliased it with _ (like this is commonly done with gettext_lazy). If I do not use the alias, everything works normally... So, it looks like the way pgettext_lazy works depends on the way it's imported. Is that even possible?

I don't think so. This can be an issue in your code. You can try to prepare a minimum, sample project that reproduces your issue and ask folks on one of support channels for help in debugging.

comment:5 by David Sanders, 19 months ago

Hi Kamil,

The project you posted is not configured correctly. LOCALE_PATHS must be a list of directories, not a single directory.

@felixx
I think part of the problem here is that the error message highlighting this misconfiguration wasn't being shown when running makemessages, I could only see it when I simply ran ./manage.py with no args. I'm unfamiliar with makemessages but should it be showing errors? 🤔 Note that if I run ./manage.py showmigrations (for eg) it does show the error.

in reply to:  5 comment:6 by Kamil Paduszyński, 19 months ago

Sorry for that silly config error.

I have just fixed it and pushed the changes. This did not resolve the issue though.

Replying to David Sanders:

Hi Kamil,

The project you posted is not configured correctly. LOCALE_PATHS must be a list of directories, not a single directory.

@felixx
I think part of the problem here is that the error message highlighting this misconfiguration wasn't being shown when running makemessages, I could only see it when I simply ran ./manage.py with no args. I'm unfamiliar with makemessages but should it be showing errors? 🤔 Note that if I run ./manage.py showmigrations (for eg) it does show the error.

comment:7 by Claude Paroz, 19 months ago

I'm pretty sure you can't simply alias the (n|p)gettext family like you can do with the simpler gettext/gettext_lazy calls. Remember that gettext works by parsing code. So this is not a Django-specific issue, it's how gettext works. See also the --keyword xgettext parameters in makemessages.py. If aliases are used, then those aliases should also be present in some --keyword parameter (which are not customizable in Django currently).

in reply to:  7 comment:8 by Kamil Paduszyński, 19 months ago

Thanks! Now it's clearer to me. Is that "aliasing limitation thing" mentioned in docs? Maybe it should.

Replying to Claude Paroz:

I'm pretty sure you can't simply alias the (n|p)gettext family like you can do with the simpler gettext/gettext_lazy calls. Remember that gettext works by parsing code. So this is not a Django-specific issue, it's how gettext works. See also the --keyword xgettext parameters in makemessages.py. If aliases are used, then those aliases should also be present in some --keyword parameter (which are not customizable in Django currently).

comment:9 by Antoine Humbert, 19 months ago

This is clearly mentioned in documentation that you cannot alias non-single argument translation functions with _

See https://docs.djangoproject.com/en/4.2/topics/i18n/translation/

What functions may be aliased as _?

Because of how xgettext (used by makemessages) works, only functions that take a single string argument can be imported as _:

gettext()
gettext_lazy()

in reply to:  9 comment:10 by Kamil Paduszyński, 19 months ago

Thank you guys for all the help! And sorry for bothering. Next time I will dive deeper into docs.

Replying to Antoine Humbert:

This is clearly mentioned in documentation that you cannot alias non-single argument translation functions with _

See https://docs.djangoproject.com/en/4.2/topics/i18n/translation/

What functions may be aliased as _?

Because of how xgettext (used by makemessages) works, only functions that take a single string argument can be imported as _:

gettext()
gettext_lazy()

Note: See TracTickets for help on using tickets.
Back to Top