This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add gettext.pgettext() and variants support
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: AndrewZiem, Felix Schwarz, Flimm, Hanno.Zulla, Jonathan Schoonhoven, Leonid Suprun, Tristan.Fisher, berker.peksag, bernie, bersace, bronger, cheryl.sabella, cito, clouserw, dns, dwayne, eric.araujo, eschwartz, franz_glasner, genepi, loewis, mitar, nh2, nhooey, nils, olivier-berten, sascha_silbe, serhiy.storchaka, wichert, ztane
Priority: normal Keywords: needs review, patch

Created on 2008-03-29 02:10 by genepi, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
gettext-pgettext.patch franz_glasner, 2008-04-15 20:17 Patch for gettext module to add pgettext and variants review
pgettext-3.2.diff eric.araujo, 2010-11-26 05:28
Pull Requests
URL Status Linked Edit
PR 7253 merged cheryl.sabella, 2018-05-30 20:12
Messages (33)
msg64675 - (view) Author: Pierre Metras (genepi) Date: 2008-03-29 02:10
Please add support for pgettext(msgctxt, msgid) and variants (dpgettext,
dcpgettext...) in the gettext module.

I will not rephrase the justification for these functions and why
contexts are essential for good localization:
http://www.gnu.org/software/gettext/manual/gettext.html#Contexts
msg64685 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-03-29 08:38
Would you like to work on a patch?
msg65526 - (view) Author: Franz Glasner (franz_glasner) Date: 2008-04-15 20:17
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.
msg79609 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-01-11 18:57
I don't see any change in Modules/_localemodule.c: you reimplemented 
pgettext() in pure Python. Why no reusing existing pgettext() function 
(at least when it's present)?
msg79610 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-01-11 19:00
The gettext module is intentionally written in pure Python; it should
stay that way. Whether or not the _locale module should also grow
support for pgettext is a different issue.
msg90338 - (view) Author: David Schnur (dns) Date: 2009-07-09 18:14
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.
msg90339 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-07-09 18:31
Only my lack of time prevents inclusion.
msg91472 - (view) Author: Mitar (mitar) * Date: 2009-08-11 09:56
I would just like to add that I am also looking forwards to this feature.
msg92796 - (view) Author: Niklas Hambüchen (nh2) Date: 2009-09-17 21:19
Me too. This makes developing applications with good localizations in
Python really difficult.
msg97788 - (view) Author: David D Lowe (Flimm) Date: 2010-01-14 21:18
Same here.
msg105126 - (view) Author: Olivier Berten (olivier-berten) Date: 2010-05-06 06:49
Pleeeeeeeaaaaaaaase ;-)
msg106121 - (view) Author: Bernie H. Innocenti (bernie) Date: 2010-05-20 00:07
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)
msg106132 - (view) Author: Wil Clouser (clouserw) Date: 2010-05-20 04:47
Yes.  You can see an in-production implementation at http://github.com/clouserw/tower/blob/master/tower/__init__.py#L51 (I'm overriding ugettext to support an optional context).
msg106709 - (view) Author: Wichert Akkerman (wichert) Date: 2010-05-29 06:39
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.
msg107560 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-06-11 16:44
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. :)
msg122439 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-26 05:28
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?
msg122446 - (view) Author: Wichert Akkerman (wichert) Date: 2010-11-26 09:20
I can help test changes for python 2.x. The python 3.x ecosystem is at least a year away from becoming interesting for me I'm afraid.
msg122482 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-26 23:13
New features don’t go into stable branches.

The patch is also reviewable at http://codereview.appspot.com/3340041
msg202189 - (view) Author: Neil Hooey (nhooey) Date: 2013-11-05 03:13
Can someone review the patch and consider its inclusion?
msg230537 - (view) Author: Wichert Akkerman (wichert) Date: 2014-11-03 14:49
Bump.

Python 3 is still not on my radar, but I'll happily do a backport for Py2 and drop that on PyPI once this gets resolved.
msg234853 - (view) Author: Hanno Zulla (Hanno.Zulla) Date: 2015-01-27 21:02
Can we please get pgettext for Python?
msg277652 - (view) Author: Antti Haapala (ztane) * Date: 2016-09-28 19:05
*wow* isn't this in Python 3 yet (I've been using Zope translationstrings lately so I didn't know that pgettext is still not supported).
msg302801 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-23 19:25
This looks like a nice feature. Éric, could you please create a PR on GitHub?

I think that new l*gettext functions are not needed. They are rather useless in Python 3 (see issue29755).
msg306594 - (view) Author: Jonathan Schoonhoven (Jonathan Schoonhoven) Date: 2017-11-20 22:30
Is there anything I can do to help get this into the codebase and out issue purgatory? We're not that far off from the 10 year anniversary of this issue.
msg306600 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2017-11-20 23:39
The patch would need to be applied on top of the current master to make a pull request.  Then, the tasks outlined in that message should be addressed:

https://bugs.python.org/issue2504#msg122439
msg317139 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-05-19 21:48
Éric,

Was your last comment intended as a todo for yourself or an outline of the steps needed for someone else to move this along (maybe it was a reply to Jonathan Schoonhoven's question)?

If the latter, I'd be happy to try to work on the PR.

Thanks!
msg317300 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2018-05-22 15:40
It was an answer to «Is there anything I can do to help get this into the codebase»

Feel free to take this on!  I’ll try to review.
msg318206 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-05-30 20:17
I've created a pull request for this based on Éric's patch in 2010.  I attempted to incorporate the changes from #29755 into the new functions and tests.  I believe these changes take care of items 1 and 2 from msg122439.

I haven't tried to address item 3 in msg122439 yet.

Since this is already a large change, item 4 in msg122439 might be better as its own issue.
msg318261 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-31 06:33
Do you have a use case for lpgettext() and like?
msg318282 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-05-31 10:36
No, I didn't have a use case for the l* functions.  I included them just as a result of applying the entire patch from 2010.  I also didn't know if further discussion was needed before excluding them.
msg328643 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-27 05:35
l*gettext() functions have been just deprecated in issue33710. Please update the PR and remove new l*gettext() functions.
msg329417 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-07 14:12
New changeset 637a33b99685fd5d1032670fbe29c7c8a8f0ff63 by Serhiy Storchaka (Cheryl Sabella) in branch 'master':
bpo-2504: Add pgettext() and variants to gettext. (GH-7253)
https://github.com/python/cpython/commit/637a33b99685fd5d1032670fbe29c7c8a8f0ff63
msg357389 - (view) Author: Eli Schwartz (eschwartz) * Date: 2019-11-24 05:01
Interestingly enough, the final accepted patch (and the 2010 one) also fixes a bug where gettext.install(..., names='ngettext') would incorrectly work, in violation of the documentation.

I think it would also incorrectly install 'gettext', too...

It used to just check if names.__contains__ is a valid attribute, then check if 'foo' in names, so a dumb string "worked" and even matched more things than it should have.

How I discovered this: I fixed a bug in a python project that stopped working on python 3.8, and had a bit of a head-scratch regarding how it ever worked to begin with: https://github.com/linuxmint/cinnamon/pull/8964
History
Date User Action Args
2022-04-11 14:56:32adminsetgithub: 46756
2019-11-24 05:01:50eschwartzsetnosy: + eschwartz
messages: + msg357389
2018-11-07 14:32:50serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: needs patch -> resolved
2018-11-07 14:12:24serhiy.storchakasetmessages: + msg329417
2018-10-27 05:35:04serhiy.storchakasetmessages: + msg328643
versions: + Python 3.8, - Python 3.7
2018-05-31 10:36:24cheryl.sabellasetmessages: + msg318282
2018-05-31 06:33:26serhiy.storchakasetmessages: + msg318261
2018-05-30 20:17:47cheryl.sabellasetmessages: + msg318206
stage: patch review -> needs patch
2018-05-30 20:12:59cheryl.sabellasetstage: needs patch -> patch review
pull_requests: + pull_request6882
2018-05-22 15:40:09eric.araujosetmessages: + msg317300
2018-05-19 21:48:16cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg317139
2017-11-20 23:39:34eric.araujosetassignee: loewis ->
messages: + msg306600
2017-11-20 22:30:57Jonathan Schoonhovensetnosy: + Jonathan Schoonhoven
messages: + msg306594
2017-09-23 19:25:33serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg302801
2017-09-19 16:21:59eric.araujosetkeywords: + needs review
versions: + Python 3.7, - Python 3.5
2016-11-16 13:44:51Tristan.Fishersetnosy: + Tristan.Fisher
2016-09-28 19:05:20ztanesetnosy: + ztane
messages: + msg277652
2016-09-28 18:33:20Leonid Suprunsetnosy: + Leonid Suprun
2015-01-27 21:13:29berker.peksagsetnosy: + berker.peksag
2015-01-27 21:02:13Hanno.Zullasetnosy: + Hanno.Zulla
messages: + msg234853
2014-11-03 14:56:08eric.araujosetstage: patch review -> needs patch
versions: - Python 3.2, Python 3.3, Python 3.4, Python 3.6
2014-11-03 14:49:41wichertsetmessages: + msg230537
versions: + Python 3.3, Python 3.4, Python 3.5, Python 3.6
2013-11-05 03:13:03nhooeysetnosy: + nhooey
messages: + msg202189
2011-02-11 17:28:31sascha_silbesetnosy: + sascha_silbe
2011-01-31 19:15:02bersacesetnosy: + Felix Schwarz
2011-01-31 19:14:36bersacesetnosy: + bersace
2011-01-31 19:14:17bersacesetnosy: - Felix Schwarz
2010-12-26 17:05:45Felix Schwarzsetnosy: + Felix Schwarz
2010-11-26 23:13:57eric.araujosetmessages: + msg122482
2010-11-26 09:20:22wichertsetmessages: + msg122446
2010-11-26 05:28:48eric.araujosetfiles: + pgettext-3.2.diff
resolution: accepted -> (no value)
messages: + msg122439

stage: needs patch -> patch review
2010-06-29 12:42:28eric.araujosetresolution: accepted
stage: needs patch
2010-06-15 15:25:41nilssetnosy: + nils
2010-06-11 16:44:09eric.araujosetnosy: + eric.araujo
messages: + msg107560
2010-05-29 06:39:49wichertsetnosy: + wichert
messages: + msg106709
2010-05-20 04:47:21clouserwsetmessages: + msg106132
2010-05-20 00:07:17berniesetnosy: + bernie
messages: + msg106121
2010-05-06 06:56:14loewissetversions: + Python 3.2, - Python 2.6, Python 3.0
2010-05-06 06:49:23olivier-bertensetnosy: + olivier-berten
messages: + msg105126
2010-01-14 21:18:26Flimmsetnosy: + Flimm
messages: + msg97788
2009-12-02 21:29:04clouserwsetnosy: + clouserw
2009-10-24 19:29:56AndrewZiemsetnosy: + AndrewZiem
2009-09-20 18:47:00citosetnosy: + cito
2009-09-17 21:19:32nh2setnosy: + nh2
messages: + msg92796
2009-08-11 09:56:19mitarsetnosy: + mitar
messages: + msg91472
2009-07-09 18:31:24loewissetmessages: + msg90339
2009-07-09 18:14:21dnssetnosy: + dns
messages: + msg90338
2009-03-24 23:07:27vstinnersetnosy: - vstinner
2009-01-11 19:00:36loewissetmessages: + msg79610
2009-01-11 18:57:45vstinnersetnosy: + vstinner
messages: + msg79609
2009-01-11 12:25:13dwaynesetnosy: + dwayne
2008-05-03 10:22:28brongersetnosy: + bronger
2008-04-15 21:17:01loewissetassignee: loewis
2008-04-15 20:17:20franz_glasnersetfiles: + gettext-pgettext.patch
nosy: + franz_glasner
keywords: + patch
messages: + msg65526
components: + Library (Lib), - Extension Modules
2008-03-29 08:38:24loewissetnosy: + loewis
messages: + msg64685
2008-03-29 02:10:23genepicreate