diff --git a/Doc/library/gettext.rst b/Doc/library/gettext.rst --- a/Doc/library/gettext.rst +++ b/Doc/library/gettext.rst @@ -63,6 +63,15 @@ class-based API instead. :func:`_` in the local namespace (see examples below). +.. function:: pgettext(context, message) + + Return the localized translation of *message*, based on the message + *context*, the current global domain, language, and locale + directory. + + .. versionadded:: 3.2 + + .. function:: lgettext(message) Equivalent to :func:`gettext`, but the translation is returned in the @@ -70,11 +79,27 @@ class-based API instead. :func:`bind_textdomain_codeset`. +.. function:: lpgettext(context, message) + + Equivalent to :func:`pgettext`, but the translation is returned in the + preferred system encoding, if no other encoding was explicitly set with + :func:`bind_textdomain_codeset`. + + .. versionadded:: 3.2 + + .. function:: dgettext(domain, message) Like :func:`gettext`, but look the message up in the specified *domain*. +.. function:: dpgettext(domain, context, message) + + Like :func:`pgettext`, but look the message up in the specified *domain*. + + .. versionadded:: 3.2 + + .. function:: ldgettext(domain, message) Equivalent to :func:`dgettext`, but the translation is returned in the @@ -82,6 +107,15 @@ class-based API instead. :func:`bind_textdomain_codeset`. +.. function:: ldpgettext(domain, context, message) + + Equivalent to :func:`dpgettext`, but the translation is returned in the + preferred system encoding, if no other encoding was explicitly set with + :func:`bind_textdomain_codeset`. + + .. versionadded:: 3.2 + + .. function:: ngettext(singular, plural, n) Like :func:`gettext`, but consider plural forms. If a translation is found, @@ -96,6 +130,22 @@ class-based API instead. languages. +.. function:: npgettext(context, singular, plural, n) + + Like :func:`pgettext`, but consider plural forms. If a translation is found, + apply the plural formula to *n*, and return the resulting message (some + languages have more than two plural forms). If no translation is found, + return *singular* if *n* is 1; return *plural* otherwise. + + The plural formula is taken from the catalog header. It is a C or Python + expression that has a free variable *n*; the expression evaluates to the + index of the plural in the catalog. See the GNU :program:`gettext` + documentation for the precise syntax to be used in :file:`.po` files and the + formulas for a variety of languages. + + .. versionadded:: 3.2 + + .. function:: lngettext(singular, plural, n) Equivalent to :func:`ngettext`, but the translation is returned in the @@ -103,11 +153,27 @@ class-based API instead. :func:`bind_textdomain_codeset`. +.. function:: lnpgettext(context, singular, plural, n) + + Equivalent to :func:`npgettext`, but the translation is returned in the + preferred system encoding, if no other encoding was explicitly set with + :func:`bind_textdomain_codeset`. + + .. versionadded:: 3.2 + + .. function:: dngettext(domain, singular, plural, n) Like :func:`ngettext`, but look the message up in the specified *domain*. +.. function:: dnpgettext(domain, context, singular, plural, n) + + Like :func:`npgettext`, but look the message up in the specified *domain*. + + .. versionadded:: 3.2 + + .. function:: ldngettext(domain, singular, plural, n) Equivalent to :func:`dngettext`, but the translation is returned in the @@ -115,6 +181,15 @@ class-based API instead. :func:`bind_textdomain_codeset`. +.. function:: ldnpgettext(domain, context, singular, plural, n) + + Equivalent to :func:`dnpgettext`, but the translation is returned in the + preferred system encoding, if no other encoding was explicitly set with + :func:`bind_textdomain_codeset`. + + .. versionadded:: 3.2 + + Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but this was deemed not useful and so it is currently unimplemented. @@ -246,11 +321,29 @@ are the methods of :class:`NullTranslati Otherwise, return the translated message. Overridden in derived classes. + .. method:: pgettext(context, message) + + If a fallback has been set, forward :meth:`pgettext` to the fallback. + Otherwise, return the translated message. Overridden in derived classes. + + .. versionadded:: 3.2 + + .. method:: lgettext(message) If a fallback has been set, forward :meth:`lgettext` to the fallback. Otherwise, return the translated message. Overridden in derived classes. + .. versionadded:: 3.2 + + + .. method:: lpgettext(context, message) + + If a fallback has been set, forward :meth:`lgettext` to the fallback. + Otherwise, return the translated message. Overriden in derived classes. + + .. versionadded:: 3.2 + .. method:: ngettext(singular, plural, n) @@ -258,12 +351,28 @@ are the methods of :class:`NullTranslati Otherwise, return the translated message. Overridden in derived classes. + .. method:: npgettext(context, singular, plural, n) + + If a fallback has been set, forward :meth:`npgettext` to the fallback. + Otherwise, return the translated message. Overridden in derived classes. + + .. versionadded:: 3.2 + + .. method:: lngettext(singular, plural, n) If a fallback has been set, forward :meth:`ngettext` to the fallback. Otherwise, return the translated message. Overridden in derived classes. + .. method:: lnpgettext(context, singular, plural, n) + + If a fallback has been set, forward :meth:`lnpgettext` to the fallback. + Otherwise, return the translated message. Overridden in derived classes. + + .. versionadded:: 3.2 + + .. method:: info() Return the "protected" :attr:`_info` variable. @@ -297,7 +406,8 @@ are the methods of :class:`NullTranslati names of functions you want to install in the builtins namespace in addition to :func:`_`. Supported names are ``'gettext'`` (bound to :meth:`self.gettext`), ``'ngettext'`` (bound to :meth:`self.ngettext`), - ``'lgettext'`` and ``'lngettext'``. + ``'pgettext'``, ``'lgettext'``, ``'lpgettext'``, ``'lngettext'`` and + ``'lnpgettext'``. Note that this is only one way, albeit the most convenient way, to make the :func:`_` function available to your application. Because it affects @@ -352,6 +462,18 @@ The following methods are overridden fro fallback's :meth:`gettext` method. Otherwise, the *message* id is returned. +.. method:: GNUTranslations.pgettext(context, message) + + Look up the *context* and *message* id in the catalog and return the + corresponding message string, as an 8-bit string encoded with the catalog's + encoding, if known. If there is no entry in the catalog for the *message* + id and *context*, and a fallback has been set, the look up is forwarded to + the fallback's :meth:`pgettext` method. Otherwise, the *message* id is + returned. + + .. versionadded:: 3.2 + + .. method:: GNUTranslations.lgettext(message) Equivalent to :meth:`gettext`, but the translation is returned as a @@ -359,6 +481,15 @@ The following methods are overridden fro encoding if no encoding was explicitly set with :meth:`set_output_charset`. +.. method:: GNUTranslations.lpgettext(context, message) + + Equivalent to :meth:`pgettext`, but the translation is returned in the + preferred system encoding, if no other encoding was explicitly set with + :meth:`set_output_charset`. + + .. versionadded:: 3.2 + + .. method:: GNUTranslations.ngettext(singular, plural, n) Do a plural-forms lookup of a message id. *singular* is used as the message id @@ -379,6 +510,20 @@ The following methods are overridden fro n) % {'num': n} +.. method:: GNUTranslations.npgettext(context, singular, plural, n) + + Do a plural-forms lookup of a message id. *singular* is used as the message + id for purposes of lookup in the catalog, while *n* is used to determine + which plural form to use. The returned message string is an 8-bit string + encoded with the catalog's encoding, if known. + + If the message id for *context* is not found in the catalog, and a fallback is specified, the + request is forwarded to the fallback's :meth:`npgettext` method. Otherwise, when + *n* is 1 *singular* is returned, and *plural* is returned in all other cases. + + .. versionadded:: 3.2 + + .. method:: GNUTranslations.lngettext(singular, plural, n) Equivalent to :meth:`gettext`, but the translation is returned as a @@ -386,6 +531,15 @@ The following methods are overridden fro encoding if no encoding was explicitly set with :meth:`set_output_charset`. +.. method:: GNUTranslations.lnpgettext(context, singular, plural, n) + + Equivalent to :meth:`pgettext`, but the translation is returned in the + preferred system encoding, if no other encoding was explicitly set with + :meth:`set_output_charset`. + + .. versionadded:: 3.2 + + Solaris message catalog support ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/Lib/gettext.py b/Lib/gettext.py --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -53,6 +53,7 @@ from errno import ENOENT __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', 'find', 'translation', 'install', 'textdomain', 'bindtextdomain', 'dgettext', 'dngettext', 'gettext', 'ngettext', + 'pgettext', 'lpgettext', 'npgettext', 'lnpgettext', 'ldnpgettext', ] _default_localedir = os.path.join(sys.prefix, 'share', 'locale') @@ -189,11 +190,21 @@ class NullTranslations: return self._fallback.gettext(message) return message + def pgettext(self, context, message): + if self._fallback: + return self._fallback.pgettext(context, message) + return message + def lgettext(self, message): if self._fallback: return self._fallback.lgettext(message) return message + def lpgettext(self, context, message): + if self._fallback: + return self._fallback.lpgettext(context, message) + return message + def ngettext(self, msgid1, msgid2, n): if self._fallback: return self._fallback.ngettext(msgid1, msgid2, n) @@ -202,6 +213,14 @@ class NullTranslations: else: return msgid2 + def npgettext(self, context, msgid1, msgid2, n): + if self._fallback: + return self._fallback.npgettext(context, msgid1, msgid2, n) + if n == 1: + return msgid1 + else: + return msgid2 + def lngettext(self, msgid1, msgid2, n): if self._fallback: return self._fallback.lngettext(msgid1, msgid2, n) @@ -210,6 +229,14 @@ class NullTranslations: else: return msgid2 + def lnpgettext(self, context, msgid1, msgid2, n): + if self._fallback: + return self._fallback.lnpgettext(context, msgid1, msgid2, n) + if n == 1: + return msgid1 + else: + return msgid2 + def info(self): return self._info @@ -225,15 +252,11 @@ class NullTranslations: def install(self, names=None): import builtins builtins.__dict__['_'] = self.gettext - if hasattr(names, "__contains__"): - if "gettext" in names: - builtins.__dict__['gettext'] = builtins.__dict__['_'] - if "ngettext" in names: - builtins.__dict__['ngettext'] = self.ngettext - if "lgettext" in names: - builtins.__dict__['lgettext'] = self.lgettext - if "lngettext" in names: - builtins.__dict__['lngettext'] = self.lngettext + if names is not None: + allowed = {'gettext', 'lgettext', 'lngettext', 'lnpgettext', + 'lpgettext', 'ngettext', 'npgettext', 'pgettext'} + for name in allowed & set(names): + builtins.__dict__[name] = getattr(self, name) class GNUTranslations(NullTranslations): @@ -241,6 +264,10 @@ class GNUTranslations(NullTranslations): LE_MAGIC = 0x950412de BE_MAGIC = 0xde120495 + # The encoding of a msgctxt and a msgid in a .mo file is + # msgctxt + "\x04" + msgid (gettext version >= 0.15) + CONTEXT = "%s\x04%s" + def _parse(self, fp): """Override this method to support alternative .mo formats.""" unpack = struct.unpack @@ -318,6 +345,21 @@ class GNUTranslations(NullTranslations): masteridx += 8 transidx += 8 + def pgettext(self, context, message): + ctxt_msg_id = self.CONTEXT % (context, message) + missing = object() + tmsg = self._catalog.get(ctxt_msg_id, missing) + if tmsg is missing: + if self._fallback: + return self._fallback.pgettext(context, message) + return message + # Encode the Unicode tmsg back to an 8-bit string, if possible + if self._output_charset: + return tmsg.encode(self._output_charset) + elif self._charset: + return tmsg.encode(self._charset) + return tmsg + def lgettext(self, message): missing = object() tmsg = self._catalog.get(message, missing) @@ -329,6 +371,23 @@ class GNUTranslations(NullTranslations): return tmsg.encode(self._output_charset) return tmsg.encode(locale.getpreferredencoding()) + def npgettext(self, context, msgid1, msgid2, n): + ctxt_msg_id = self.CONTEXT % (context, msgid1) + try: + tmsg = self._catalog[(ctxt_msg_id, self.plural(n))] + if self._output_charset: + return tmsg.encode(self._output_charset) + elif self._charset: + return tmsg.encode(self._charset) + return tmsg + except KeyError: + if self._fallback: + return self._fallback.npgettext(context, msgid1, msgid2, n) + if n == 1: + return msgid1 + else: + return msgid2 + def lngettext(self, msgid1, msgid2, n): try: tmsg = self._catalog[(msgid1, self.plural(n))] @@ -352,6 +411,18 @@ class GNUTranslations(NullTranslations): return message return tmsg + def lpgettext(self, context, message): + ctxt_msg_id = self.CONTEXT % (context, message) + missing = object() + tmsg = self._catalog.get(ctxt_msg_id, missing) + if tmsg is missing: + if self._fallback: + return self._fallback.lpgettext(context, message) + return message + if self._output_charset: + return tmsg.encode(self._output_charset) + return tmsg.encode(locale.getpreferredencoding()) + def ngettext(self, msgid1, msgid2, n): try: tmsg = self._catalog[(msgid1, self.plural(n))] @@ -364,6 +435,21 @@ class GNUTranslations(NullTranslations): tmsg = msgid2 return tmsg + def lnpgettext(self, context, msgid1, msgid2, n): + ctxt_msg_id = self.CONTEXT % (context, msgid1) + try: + tmsg = self._catalog[(ctxt_msg_id, self.plural(n))] + if self._output_charset: + return tmsg.encode(self._output_charset) + return tmsg.encode(locale.getpreferredencoding()) + except KeyError: + if self._fallback: + return self._fallback.lnpgettext(context, msgid1, msgid2, n) + if n == 1: + return msgid1 + else: + return msgid2 + # Locate a .mo file using the gettext strategy def find(domain, localedir=None, languages=None, all=False): @@ -480,6 +566,14 @@ def dgettext(domain, message): return message return t.gettext(message) +def dpgettext(domain, context, message): + try: + t = translation(domain, _localedirs.get(domain, None), + codeset=_localecodesets.get(domain)) + except IOError: + return message + return t.pgettext(context, message) + def ldgettext(domain, message): try: t = translation(domain, _localedirs.get(domain, None), @@ -488,6 +582,14 @@ def ldgettext(domain, message): return message return t.lgettext(message) +def ldpgettext(domain, context, message): + try: + t = translation(domain, _localedirs.get(domain, None), + codeset=_localecodesets.get(domain)) + except IOError: + return message + return t.lpgettext(context, message) + def dngettext(domain, msgid1, msgid2, n): try: t = translation(domain, _localedirs.get(domain, None), @@ -499,6 +601,17 @@ def dngettext(domain, msgid1, msgid2, n) return msgid2 return t.ngettext(msgid1, msgid2, n) +def dnpgettext(domain, context, msgid1, msgid2, n): + try: + t = translation(domain, _localedirs.get(domain, None), + codeset=_localecodesets.get(domain)) + except IOError: + if n == 1: + return msgid1 + else: + return msgid2 + return t.npgettext(context, msgid1, msgid2, n) + def ldngettext(domain, msgid1, msgid2, n): try: t = translation(domain, _localedirs.get(domain, None), @@ -510,18 +623,41 @@ def ldngettext(domain, msgid1, msgid2, n return msgid2 return t.lngettext(msgid1, msgid2, n) +def ldnpgettext(domain, context, msgid1, msgid2, n): + try: + t = translation(domain, _localedirs.get(domain, None), + codeset=_localecodesets.get(domain)) + except IOError: + if n == 1: + return msgid1 + else: + return msgid2 + return t.lnpgettext(context, msgid1, msgid2, n) + def gettext(message): return dgettext(_current_domain, message) +def pgettext(context, message): + return dpgettext(_current_domain, context, message) + def lgettext(message): return ldgettext(_current_domain, message) +def lpgettext(context, message): + return ldpgettext(_current_domain, context, message) + def ngettext(msgid1, msgid2, n): return dngettext(_current_domain, msgid1, msgid2, n) +def npgettext(context, msgid1, msgid2, n): + return dnpgettext(_current_domain, context, msgid1, msgid2, n) + def lngettext(msgid1, msgid2, n): return ldngettext(_current_domain, msgid1, msgid2, n) +def lnpgettext(context, msgid1, msgid2, n): + return ldnpgettext(_current_domain, context, msgid1, msgid2, n) + # dcgettext() has been deemed unnecessary and is not implemented. # James Henstridge's Catalog constructor from GNOME gettext. Documented usage diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -14,33 +14,37 @@ from test import support # - Tests should have only one assert. GNU_MO_DATA = b'''\ -3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj -AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD -AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAA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WFudWFsbHkKAMKkeXoA +3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj +AAAABAAAAHMBAAAWAAAAeAEAAABhYsOeAG15Y29udGV4dMOeBGFiw54AUHJvamVjdC1JZC1WZXJz +aW9uOiAyLjAKUE8tUmV2aXNpb24tRGF0ZTogMjAwMy0wNC0xMSAxMjo0Mi0wNDAwCkxhc3QtVHJh +bnNsYXRvcjogQmFycnkgQS4gV0Fyc2F3IDxiYXJyeUBweXRob24ub3JnPgpMYW5ndWFnZS1UZWFt +OiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5 +cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9dXRmLTgKQ29udGVudC1UcmFuc2Zlci1FbmNvZGluZzog +N2JpdApHZW5lcmF0ZWQtQnk6IG1hbnVhbGx5CgDCpHl6AMKkeXogKGNvbnRleHQgdmVyc2lvbikA ''' MMO_DATA = b'''\ @@ -85,7 +89,7 @@ class GettextTestCase1(GettextBaseTest): GettextBaseTest.setUp(self) self.localedir = os.curdir self.mofile = MOFILE - gettext.install('gettext', self.localedir) + gettext.install('gettext', self.localedir, names=['pgettext']) def test_some_translations(self): eq = self.assertEqual @@ -95,6 +99,13 @@ class GettextTestCase1(GettextBaseTest): eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove') eq(_(r'nudge nudge'), 'wink wink') + def test_some_translations_with_context(self): + eq = self.assertEqual + eq(pgettext('my context', 'nudge nudge'), + 'wink wink (in "my context")') + eq(pgettext('my other context', 'nudge nudge'), + 'wink wink (in "my other context")') + def test_double_quotes(self): eq = self.assertEqual # double quotes @@ -142,12 +153,18 @@ trggrkg zrffntr pngnybt yvoenel.''') eq(_('mullusk'), 'bacon') # Test installation of other methods import builtins - t.install(names=["gettext", "lgettext"]) + saved_names = sorted(dir(builtins)) + t.install(names=["gettext", "lgettext", "pgettext", "lpgettext"]) eq(_, t.gettext) eq(builtins.gettext, t.gettext) eq(lgettext, t.lgettext) + eq(pgettext, t.pgettext) + eq(lpgettext, t.lpgettext) del builtins.gettext del builtins.lgettext + del builtins.pgettext + del builtins.lpgettext + eq(sorted(dir(builtins)), saved_names) class GettextTestCase2(GettextBaseTest): @@ -174,6 +191,20 @@ class GettextTestCase2(GettextBaseTest): eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove') eq(self._(r'nudge nudge'), 'wink wink') + def test_some_translations_with_context(self): + eq = self.assertEqual + eq(gettext.pgettext('my context', 'nudge nudge'), + 'wink wink (in "my context")') + eq(gettext.pgettext('my other context', 'nudge nudge'), + 'wink wink (in "my other context")') + + def test_some_translations_with_context_and_domain(self): + eq = self.assertEqual + eq(gettext.dpgettext('gettext', 'my context', 'nudge nudge'), + 'wink wink (in "my context")') + eq(gettext.dpgettext('gettext', 'my other context', 'nudge nudge'), + 'wink wink (in "my other context")') + def test_double_quotes(self): eq = self.assertEqual # double quotes @@ -209,6 +240,89 @@ fhccbeg sbe lbhe Clguba cebtenzf ol cebi trggrkg zrffntr pngnybt yvoenel.''') +class GettextTestCase3(GettextBaseTest): + # lgettext and variants + def setUp(self): + GettextBaseTest.setUp(self) + self.localedir = os.curdir + # Set up the bindings + gettext.bindtextdomain('gettext', self.localedir) + gettext.textdomain('gettext') + # Convert back from gettext-internal Unicode + gettext.bind_textdomain_codeset('gettext', 'iso-8859-1') + + def test_some_translations(self): + eq = self.assertEqual + unless = self.assertTrue + t = gettext.lgettext('nudge nudge') + unless(isinstance(t, str)) + eq(t, 'wink wink') + + def test_some_translations_with_context(self): + unless = self.assertTrue + eq = self.assertEqual + t = gettext.lpgettext('my context', 'nudge nudge') + unless(isinstance(t, str)) + eq(t, 'wink wink (in "my context")') + + def test_some_plural_translations(self): + unless = self.assertTrue + eq = self.assertEqual + t = gettext.lngettext('There is %s file', 'There are %s files', 1) + unless(isinstance(t, str)) + eq(t, 'Hay %s fichero') + t = gettext.lngettext('There is %s file', 'There are %s files', 2) + unless(isinstance(t, str)) + eq(t, 'Hay %s ficheros') + + def test_some_plural_translations_with_context(self): + unless = self.assertTrue + eq = self.assertEqual + t = gettext.lnpgettext('With context', + 'There is %s file', 'There are %s files', 1) + unless(isinstance(t, str)) + eq(t, 'Hay %s fichero (context)') + t = gettext.lnpgettext('With context', + 'There is %s file', 'There are %s files', 2) + unless(isinstance(t, str)) + eq(t, 'Hay %s ficheros (context)') + + +class GettextTestCase4(GettextBaseTest): + # dgettext and variants + def setUp(self): + GettextBaseTest.setUp(self) + + def test_some_translations_with_domain(self): + eq = self.assertEqual + eq(gettext.dgettext('gettext', 'nudge nudge'), 'wink wink') + + def test_some_plural_translations_with_domain(self): + eq = self.assertEqual + eq(gettext.dngettext('gettext', + 'There is %s file', 'There are %s files', 1), + 'Hay %s fichero') + eq(gettext.dngettext('gettext', + 'There is %s file', 'There are %s files', 2), + 'Hay %s ficheros') + + def test_some_translations_with_context_and_domain(self): + eq = self.assertEqual + eq(gettext.dpgettext('gettext', 'my context', 'nudge nudge'), + 'wink wink (in "my context")') + eq(gettext.dpgettext('gettext', 'my other context', 'nudge nudge'), + 'wink wink (in "my other context")') + + def test_some_plural_translations_with_domain_and_context(self): + eq = self.assertEqual + eq(gettext.dnpgettext('gettext', 'With context', + 'There is %s file', 'There are %s files', 1), + 'Hay %s fichero (context)') + eq(gettext.dnpgettext('gettext', 'With context', + 'There is %s file', 'There are %s files', 2), + 'Hay %s ficheros (context)') + + class PluralFormsTestCase(GettextBaseTest): def setUp(self): GettextBaseTest.setUp(self) @@ -221,6 +335,15 @@ class PluralFormsTestCase(GettextBaseTes x = gettext.ngettext('There is %s file', 'There are %s files', 2) eq(x, 'Hay %s ficheros') + def test_plural_context_forms1(self): + eq = self.assertEqual + x = gettext.npgettext('With context', + 'There is %s file', 'There are %s files', 1) + eq(x, 'Hay %s fichero (context)') + x = gettext.npgettext('With context', + 'There is %s file', 'There are %s files', 2) + eq(x, 'Hay %s ficheros (context)') + def test_plural_forms2(self): eq = self.assertEqual with open(self.mofile, 'rb') as fp: @@ -230,6 +353,17 @@ class PluralFormsTestCase(GettextBaseTes x = t.ngettext('There is %s file', 'There are %s files', 2) eq(x, 'Hay %s ficheros') + def test_plural_context_forms2(self): + eq = self.assertEqual + with open(self.mofile, 'rb') as fp: + t = gettext.GNUTranslations(fp) + x = t.npgettext('With context', + 'There is %s file', 'There are %s files', 1) + eq(x, 'Hay %s fichero (context)') + x = t.npgettext('With context', + 'There is %s file', 'There are %s files', 2) + eq(x, 'Hay %s ficheros (context)') + def test_hu(self): eq = self.assertEqual f = gettext.c2py('0') @@ -297,16 +431,65 @@ class UnicodeTranslationsTest(GettextBas with open(UMOFILE, 'rb') as fp: self.t = gettext.GNUTranslations(fp) self._ = self.t.gettext + self.pgettext = self.t.pgettext def test_unicode_msgid(self): unless = self.assertTrue unless(isinstance(self._(''), str)) - unless(isinstance(self._(''), str)) def test_unicode_msgstr(self): eq = self.assertEqual eq(self._('ab\xde'), '\xa4yz') + def test_unicode_context_msgstr(self): + eq = self.assertEqual + unless = self.assertTrue + t = self.pgettext('mycontext\xde', 'ab\xde') + unless(isinstance(t, str)) + eq(t, '\xa4yz (context version)') + + +class UnicodeTranslationsPluralTest(GettextBaseTest): + def setUp(self): + GettextBaseTest.setUp(self) + with open(MOFILE, 'rb') as fp: + self.t = gettext.GNUTranslations(fp) + self.ngettext = self.t.ngettext + self.npgettext = self.t.npgettext + + def test_unicode_msgid(self): + unless = self.assertTrue + unless(isinstance(self.ngettext('', '', 1), str)) + unless(isinstance(self.ngettext('', '', 2), str)) + + def test_unicode_context_msgid(self): + unless = self.assertTrue + unless(isinstance(self.npgettext('', '', '', 1), str)) + unless(isinstance(self.npgettext('', '', '', 2), str)) + + def test_unicode_msgstr(self): + eq = self.assertEqual + unless = self.assertTrue + t = self.ngettext("There is %s file", "There are %s files", 1) + unless(isinstance(t, str)) + eq(t, "Hay %s fichero") + unless(isinstance(t, str)) + t = self.ngettext("There is %s file", "There are %s files", 5) + unless(isinstance(t, str)) + eq(t, "Hay %s ficheros") + + def test_unicode_msgstr_with_context(self): + eq = self.assertEqual + unless = self.assertTrue + t = self.npgettext("With context", + "There is %s file", "There are %s files", 1) + unless(isinstance(t, str)) + eq(t, "Hay %s fichero (context)") + t = self.npgettext("With context", + "There is %s file", "There are %s files", 5) + unless(isinstance(t, str)) + eq(t, "Hay %s ficheros (context)") + class WeirdMetadataTest(GettextBaseTest): def setUp(self): @@ -391,6 +574,14 @@ msgstr "" msgid "nudge nudge" msgstr "wink wink" +msgctxt "my context" +msgid "nudge nudge" +msgstr "wink wink (in \"my context\")" + +msgctxt "my other context" +msgid "nudge nudge" +msgstr "wink wink (in \"my other context\")" + #: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34 #: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95 msgid "albatross" @@ -423,6 +614,14 @@ msgid "There is %s file" msgid_plural "There are %s files" msgstr[0] "Hay %s fichero" msgstr[1] "Hay %s ficheros" + +# Manually added, as neither pygettext nor xgettext support plural forms +# and context in Python. +msgctxt "With context" +msgid "There is %s file" +msgid_plural "There are %s files" +msgstr[0] "Hay %s fichero (context)" +msgstr[1] "Hay %s ficheros (context)" ''' # Here's the second example po file example, used to generate the UMO_DATA @@ -447,6 +646,11 @@ msgstr "" #: nofile:0 msgid "ab\xc3\x9e" msgstr "\xc2\xa4yz" + +#: nofile:1 +msgctxt "mycontext\xc3\x9e" +msgid "ab\xc3\x9e" +msgstr "\xc2\xa4yz (context version)" ''' # Here's the third example po file, used to generate MMO_DATA diff --git a/Misc/ACKS b/Misc/ACKS --- a/Misc/ACKS +++ b/Misc/ACKS @@ -311,6 +311,7 @@ Dinu Gherman Jonathan Giddy Johannes Gijsbers Michael Gilfix +Franz Glasner Christoph Gohlke Tim Golden Chris Gonnerman diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -41,6 +41,9 @@ Core and Builtins Library ------- +- Issue #2504: Add pgettext function/method and variants to gettext. Patch by + Franz Glasner, ported by Éric Araujo. + - Issue #2986: difflib.SequenceMatcher gets a new parameter, autojunk, which can be set to False to turn off the previously undocumented 'popularity' heuristic. Patch by Terry Reedy and Eli Bendersky. diff --git a/Tools/i18n/msgfmt.py b/Tools/i18n/msgfmt.py --- a/Tools/i18n/msgfmt.py +++ b/Tools/i18n/msgfmt.py @@ -5,7 +5,8 @@ This program converts a textual Uniforum-style message catalog (.po file) into a binary GNU catalog (.mo file). This is essentially the same function as the -GNU msgfmt program, however, it is a simpler implementation. +GNU msgfmt program, however, it is a simpler implementation. Currently it +does not handle plural forms but it does handle message contexts. Usage: msgfmt.py [OPTIONS] filename.po @@ -31,7 +32,7 @@ import struct import array from email.parser import HeaderParser -__version__ = "1.1" +__version__ = "1.2" MESSAGES = {} @@ -45,11 +46,14 @@ def usage(code, msg=''): -def add(id, str, fuzzy): +def add(ctxt, id, str, fuzzy): "Add a non-fuzzy translation to the dictionary." global MESSAGES if not fuzzy and str: - MESSAGES[id] = str + if ctxt is None: + MESSAGES[id] = str + else: + MESSAGES["%s\x04%s" % (ctxt, id)] = str @@ -98,6 +102,7 @@ def generate(): def make(filename, outfile): ID = 1 STR = 2 + CTXT = 3 # Compute .mo name from .po name and arguments if filename.endswith('.po'): @@ -108,12 +113,13 @@ def make(filename, outfile): outfile = os.path.splitext(infile)[0] + '.mo' try: - lines = open(infile, 'rb').readlines() + with open(infile, 'rb') as fp: + lines = fp.readlines() except IOError as msg: print(msg, file=sys.stderr) sys.exit(1) - section = None + section = msgctxt = None fuzzy = 0 # Start off assuming Latin-1, so everything decodes without failure, @@ -127,8 +133,8 @@ def make(filename, outfile): lno += 1 # If we get a comment line after a msgstr, this is a new entry if l[0] == '#' and section == STR: - add(msgid, msgstr, fuzzy) - section = None + add(msgctxt, msgid, msgstr, fuzzy) + section = msgctxt = None fuzzy = 0 # Record a fuzzy mark if l[:2] == '#,' and 'fuzzy' in l: @@ -136,10 +142,16 @@ def make(filename, outfile): # Skip comments if l[0] == '#': continue - # Now we are in a msgid section, output previous section - if l.startswith('msgid') and not l.startswith('msgid_plural'): + # Now we are in a msgid or msgctxt section, output previous section + if l.startswith("msgctxt"): if section == STR: - add(msgid, msgstr, fuzzy) + add(msgctxt, msgid, msgstr, fuzzy) + section = CTXT + l = l[7:] + msgctxt = '' + elif l.startswith('msgid') and not l.startswith('msgid_plural'): + if section == STR: + add(msgctxt, msgid, msgstr, fuzzy) if not msgid: # See whether there is an encoding declaration p = HeaderParser() @@ -182,7 +194,9 @@ def make(filename, outfile): continue # XXX: Does this always follow Python escape semantics? l = eval(l) - if section == ID: + if section == CTXT: + msgctxt += l + elif section == ID: msgid += l.encode(encoding) elif section == STR: msgstr += l.encode(encoding) @@ -193,13 +207,14 @@ def make(filename, outfile): sys.exit(1) # Add last entry if section == STR: - add(msgid, msgstr, fuzzy) + add(msgctxt, msgid, msgstr, fuzzy) # Compute output output = generate() try: - open(outfile,"wb").write(output) + with open(outfile, 'wb') as fp: + fp.write(output) except IOError as msg: print(msg, file=sys.stderr)