I have written a patch against the current Python trunk's gettext to
add support for pgettext and friends (upgettext, npgettext, unpgettext,
...).
I have also changed Tools/i18n/msgfmt.py to recognize the "msgctxt" keyword.
Some new unittests for dgettext and lgettext and variants are also included.
If the patch will be accepted then someone should drop a message to the
maintainers of GNU gettext to add the new functions as default keywords
for xgettext. Right now you have to call xgettext with
"--keyword=pgettext:1c,2" to extract messages with context.
Tools/i18n/pygettext.py does currently not handle context variants.

I came across this ticket while looking for alternatives to Python's
gettext, since I need msgctx support. It seems a patch was supplied for
this a while back. I have never contributed to Python, and am not
familiar with your release process, but is there anything preventing its
inclusion? It would be very convenient to have this in a release version.

While we're waiting for this patch to be upstreamed, what's the best way to emulate this functionality with the current gettext module?
I'm looking at the patch and it seems that code similar to this might work?
def pgettext(ctx, msg):
return gettext(ctx + "\x04" + msg)

Martin, is there anything we can do to help get this merged? I can really use this as well.
My background here is that currently the complete zope i18n support abuses message ids as a workaround, and the result works but is very painful for translators since the original string is not immediately visible for them.

The patch needs to be updated for the 3.x trunk (py3k branch), since the last 2.x version is already rc.
Everyone, thanks for expressing interest for the development of Python, but posting “me too” messages does not further the discussion, and actually takes time from already busy people. Use cases, patches, reviews and the occasional bump (one is enough) are helpful. :)

I have ported the patch to py3k, without tests errors but with test failures. What is left to do:
1) Carefully assess bytes/str issues. Decide to change the tests or the code.
2) Re-read over the new docs.
3) Exercise the updated msgfmt.py tool, which has no tests.
4) Add support in pygettext.py.
David, Mitar, nh2, David D., Olivier, Bernie, Wil, Wichert: Do you want to help with one of those tasks?