Index: Tools/i18n/msgfmt.py =================================================================== --- Tools/i18n/msgfmt.py (revision 62354) +++ Tools/i18n/msgfmt.py (working copy) @@ -6,7 +6,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 is +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 -__version__ = "1.1" +__version__ = "1.2" MESSAGES = {} @@ -45,11 +46,14 @@ -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 @@ -99,6 +103,7 @@ def make(filename, outfile): ID = 1 STR = 2 + CTXT = 3 # Compute .mo name from .po name and arguments if filename.endswith('.po'): @@ -114,7 +119,7 @@ print >> sys.stderr, msg sys.exit(1) - section = None + section = msgctxt = None fuzzy = 0 # Parse the catalog @@ -123,8 +128,8 @@ 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: @@ -132,10 +137,16 @@ # Skip comments if l[0] == '#': continue - # Now we are in a msgid section, output previous section - if l.startswith('msgid'): + # 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'): + if section == STR: + add(msgctxt, msgid, msgstr, fuzzy) section = ID l = l[5:] msgid = msgstr = '' @@ -149,7 +160,9 @@ 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 elif section == STR: msgstr += l @@ -160,7 +173,7 @@ sys.exit(1) # Add last entry if section == STR: - add(msgid, msgstr, fuzzy) + add(msgctxt, msgid, msgstr, fuzzy) # Compute output output = generate() Index: Doc/library/gettext.rst =================================================================== --- Doc/library/gettext.rst (revision 62354) +++ Doc/library/gettext.rst (working copy) @@ -66,6 +66,13 @@ :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. + + .. function:: lgettext(message) Equivalent to :func:`gettext`, but the translation is returned in the preferred @@ -75,11 +82,23 @@ .. versionadded:: 2.4 +.. 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`. + + .. 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*. + + .. function:: ldgettext(domain, message) Equivalent to :func:`dgettext`, but the translation is returned in the preferred @@ -89,6 +108,13 @@ .. versionadded:: 2.4 +.. 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`. + + .. function:: ngettext(singular, plural, n) Like :func:`gettext`, but consider plural forms. If a translation is found, @@ -105,6 +131,20 @@ .. versionadded:: 2.3 +.. 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 gettext documentation for the precise + syntax to be used in :file:`.po` files and the formulas for a variety of + languages. + + .. function:: lngettext(singular, plural, n) Equivalent to :func:`ngettext`, but the translation is returned in the preferred @@ -114,6 +154,13 @@ .. versionadded:: 2.4 +.. 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`. + + .. function:: dngettext(domain, singular, plural, n) Like :func:`ngettext`, but look the message up in the specified *domain*. @@ -121,6 +168,11 @@ .. versionadded:: 2.3 +.. function:: dnpgettext(domain, context, singular, plural, n) + + Like :func:`npgettext`, but look the message up in the specified *domain*. + + .. function:: ldngettext(domain, singular, plural, n) Equivalent to :func:`dngettext`, but the translation is returned in the @@ -129,6 +181,13 @@ .. versionadded:: 2.4 +.. 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`. + + Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but this was deemed not useful and so it is currently unimplemented. @@ -270,6 +329,12 @@ return the translated message. Overridden in derived classes. +.. method:: NullTranslations.pgettext(context, message) + + If a fallback has been set, forward :meth:`pgettext` to the fallback. Otherwise, + return the translated message. Overridden in derived classes. + + .. method:: NullTranslations.lgettext(message) If a fallback has been set, forward :meth:`lgettext` to the fallback. Otherwise, @@ -278,6 +343,12 @@ .. versionadded:: 2.4 +.. method:: NullTranslations.lpgettext(context, message) + + If a fallback has been set, forward :meth:`lpgettext` to the fallback. Otherwise, + return the translated message. Overridden in derived classes. + + .. method:: NullTranslations.ugettext(message) If a fallback has been set, forward :meth:`ugettext` to the fallback. Otherwise, @@ -285,6 +356,13 @@ classes. +.. method:: NullTranslations.upgettext(context, message) + + If a fallback has been set, forward :meth:`upgettext` to the fallback. Otherwise, + return the translated message as a Unicode string. Overridden in derived + classes. + + .. method:: NullTranslations.ngettext(singular, plural, n) If a fallback has been set, forward :meth:`ngettext` to the fallback. Otherwise, @@ -293,14 +371,26 @@ .. versionadded:: 2.3 +.. method:: NullTranslations.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. + + .. method:: NullTranslations.lngettext(singular, plural, n) - If a fallback has been set, forward :meth:`ngettext` to the fallback. Otherwise, + If a fallback has been set, forward :meth:`lngettext` to the fallback. Otherwise, return the translated message. Overridden in derived classes. .. versionadded:: 2.4 +.. method:: NullTranslations.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. + + .. method:: NullTranslations.ungettext(singular, plural, n) If a fallback has been set, forward :meth:`ungettext` to the fallback. @@ -310,6 +400,13 @@ .. versionadded:: 2.3 +.. method:: NullTranslations.unpgettext(context, singular, plural, n) + + If a fallback has been set, forward :meth:`unpgettext` to the fallback. + Otherwise, return the translated message as a Unicode string. Overridden in + derived classes. + + .. method:: NullTranslations.info() Return the "protected" :attr:`_info` variable. @@ -345,9 +442,13 @@ If the *names* parameter is given, it must be a sequence containing the names of functions you want to install in the builtin namespace in addition to :func:`_`. Supported names are ``'gettext'`` (bound to :meth:`self.gettext` or - :meth:`self.ugettext` according to the *unicode* flag), ``'ngettext'`` (bound to - :meth:`self.ngettext` or :meth:`self.ungettext` according to the *unicode* - flag), ``'lgettext'`` and ``'lngettext'``. + :meth:`self.ugettext` according to the *unicode* flag), ``'pgettext'`` + (bound to :meth:`self.pgettext` or :meth:`self.upgettext` according to the + *unicode flag), ``'ngettext'`` (bound to :meth:`self.ngettext` or + :meth:`self.ungettext` according to the *unicode* flag), + ``'npgettext'`` (bound to :meth:`self.npgettext` or + :meth:`self.unpgettext` according to the *unicode* flag), + ``'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 the entire @@ -409,6 +510,16 @@ 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 charset 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. + + .. method:: GNUTranslations.lgettext(message) Equivalent to :meth:`gettext`, but the translation is returned in the preferred @@ -418,6 +529,13 @@ .. versionadded:: 2.4 +.. 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`. + + .. method:: GNUTranslations.ugettext(message) Look up the *message* id in the catalog and return the corresponding message @@ -426,6 +544,14 @@ fallback's :meth:`ugettext` method. Otherwise, the *message* id is returned. +.. method:: GNUTranslations.upgettext(context, message) + + Look up the *context* and *message* id in the catalog and return the corresponding message + string, as a Unicode string. 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:`upgettext` method. Otherwise, the *message* id is returned. + + .. method:: GNUTranslations.ngettext(singular, plural, n) Do a plural-forms lookup of a message id. *singular* is used as the message id @@ -440,6 +566,18 @@ .. versionadded:: 2.3 +.. 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 charset 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. + + .. method:: GNUTranslations.lngettext(singular, plural, n) Equivalent to :meth:`gettext`, but the translation is returned in the preferred @@ -449,6 +587,13 @@ .. versionadded:: 2.4 +.. 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`. + + .. method:: GNUTranslations.ungettext(singular, plural, n) Do a plural-forms lookup of a message id. *singular* is used as the message id @@ -472,6 +617,18 @@ .. versionadded:: 2.3 +.. method:: GNUTranslations.unpgettext(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 a Unicode string. + + 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:`unpgettext` method. Otherwise, + when *n* is 1 *singular* is returned, and *plural* is returned in all other + cases. + + Solaris message catalog support ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -740,6 +897,8 @@ * Gustavo Niemeyer +* Franz Glasner + .. rubric:: Footnotes .. [#] The default locale directory is system dependent; for example, on RedHat Linux Index: Lib/gettext.py =================================================================== --- Lib/gettext.py (revision 62354) +++ Lib/gettext.py (working copy) @@ -193,11 +193,21 @@ 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) @@ -206,6 +216,14 @@ 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) @@ -214,11 +232,24 @@ 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 ugettext(self, message): if self._fallback: return self._fallback.ugettext(message) return unicode(message) + def upgettext(self, context, message): + if self._fallback: + return self._fallback.upgettext(context, message) + return unicode(message) + def ungettext(self, msgid1, msgid2, n): if self._fallback: return self._fallback.ungettext(msgid1, msgid2, n) @@ -227,6 +258,14 @@ else: return unicode(msgid2) + def unpgettext(self, context, msgid1, msgid2, n): + if self._fallback: + return self._fallback.unpgettext(context, msgid1, msgid2, n) + if n == 1: + return unicode(msgid1) + else: + return unicode(msgid2) + def info(self): return self._info @@ -245,13 +284,23 @@ if hasattr(names, "__contains__"): if "gettext" in names: __builtin__.__dict__['gettext'] = __builtin__.__dict__['_'] + if "pgettext" in names: + __builtin__.__dict__['pgettext'] = (unicode and self.upgettext + or self.pgettext) if "ngettext" in names: __builtin__.__dict__['ngettext'] = (unicode and self.ungettext or self.ngettext) + if "npgettext" in names: + __builtin__.__dict__['npgettext'] = \ + (unicode and self.unpgettext or self.npgettext) if "lgettext" in names: __builtin__.__dict__['lgettext'] = self.lgettext + if "lpgettext" in names: + __builtin__.__dict__['lpgettext'] = self.lpgettext if "lngettext" in names: __builtin__.__dict__['lngettext'] = self.lngettext + if "lnpgettext" in names: + __builtin__.__dict__['lnpgettext'] = self.lnpgettext class GNUTranslations(NullTranslations): @@ -259,6 +308,10 @@ LE_MAGIC = 0x950412deL BE_MAGIC = 0xde120495L + # The encoding of a msgctxt and a msgid in a .mo file is + # msgctxt + "\x04" + msgid (gettext version >= 0.15) + CONTEXT_ENCODING = "%s\x04%s" + def _parse(self, fp): """Override this method to support alternative .mo formats.""" unpack = struct.unpack @@ -354,6 +407,21 @@ return tmsg.encode(self._charset) return tmsg + def pgettext(self, context, message): + ctxt_msg_id = self.CONTEXT_ENCODING % (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) @@ -365,6 +433,18 @@ return tmsg.encode(self._output_charset) return tmsg.encode(locale.getpreferredencoding()) + def lpgettext(self, context, message): + ctxt_msg_id = self.CONTEXT_ENCODING % (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))] @@ -381,6 +461,23 @@ else: return msgid2 + def npgettext(self, context, msgid1, msgid2, n): + ctxt_msg_id = self.CONTEXT_ENCODING % (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))] @@ -395,6 +492,21 @@ else: return msgid2 + def lnpgettext(self, context, msgid1, msgid2, n): + ctxt_msg_id = self.CONTEXT_ENCODING % (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 + def ugettext(self, message): missing = object() tmsg = self._catalog.get(message, missing) @@ -404,6 +516,16 @@ return unicode(message) return tmsg + def upgettext(self, context, message): + ctxt_message_id = self.CONTEXT_ENCODING % (context, message) + missing = object() + tmsg = self._catalog.get(ctxt_message_id, missing) + if tmsg is missing: + if self._fallback: + return self._fallback.upgettext(context, message) + return unicode(message) + return tmsg + def ungettext(self, msgid1, msgid2, n): try: tmsg = self._catalog[(msgid1, self.plural(n))] @@ -416,7 +538,20 @@ tmsg = unicode(msgid2) return tmsg + def unpgettext(self, context, msgid1, msgid2, n): + ctxt_message_id = self.CONTEXT_ENCODING % (context, msgid1) + try: + tmsg = self._catalog[(ctxt_message_id, self.plural(n))] + except KeyError: + if self._fallback: + return self._fallback.unpgettext(context, msgid1, msgid2, n) + if n == 1: + tmsg = unicode(msgid1) + else: + tmsg = unicode(msgid2) + return tmsg + # Locate a .mo file using the gettext strategy def find(domain, localedir=None, languages=None, all=0): # Get some reasonable defaults for arguments that were not supplied @@ -532,6 +667,14 @@ 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), @@ -540,6 +683,14 @@ 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), @@ -551,6 +702,17 @@ 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), @@ -562,18 +724,41 @@ 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 Index: Lib/test/test_gettext.py =================================================================== --- Lib/test/test_gettext.py (revision 62354) +++ Lib/test/test_gettext.py (working copy) @@ -14,33 +14,37 @@ # - Tests should have only one assert. GNU_MO_DATA = '''\ -3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj -AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD -AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh -eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU -aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u -CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh -Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51 -ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt -MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k -YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN -SU1FLVZlcnNpb246IDEuMApDb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dir self.mofile = MOFILE - gettext.install('gettext', self.localedir) + gettext.install('gettext', self.localedir, names=['pgettext']) def test_some_translations(self): eq = self.assertEqual @@ -99,6 +103,16 @@ eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove') eq(_(ur'nudge nudge'), 'wink wink') + def test_some_translations_with_context(self): + eq = self.assertEqual + # test some translations + eq(pgettext('my context', ur'nudge nudge'), 'wink wink (in \"my context\")') + eq(pgettext('my context', 'nudge nudge'), 'wink wink (in \"my context\")') + eq(pgettext(u'my context', 'nudge nudge'), 'wink wink (in \"my context\")') + eq(pgettext('my other context', ur'nudge nudge'), 'wink wink (in \"my other context\")') + eq(pgettext('my other context', 'nudge nudge'), 'wink wink (in \"my other context\")') + eq(pgettext(u'my other context', 'nudge nudge'), 'wink wink (in \"my other context\")') + def test_double_quotes(self): eq = self.assertEqual # double quotes @@ -147,12 +161,17 @@ eq(_('mullusk'), 'bacon') # Test installation of other methods import __builtin__ - t.install(unicode=True, names=["gettext", "lgettext"]) + t.install(unicode=True, + names=["gettext", "pgettext", "lgettext", "lpgettext"]) eq(_, t.ugettext) eq(__builtin__.gettext, t.ugettext) + eq(pgettext, t.upgettext) eq(lgettext, t.lgettext) + eq(lpgettext, t.lpgettext) del __builtin__.gettext del __builtin__.lgettext + del __builtin__.pgettext + del __builtin__.lpgettext class GettextTestCase2(GettextBaseTest): @@ -179,6 +198,38 @@ eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove') eq(self._(ur'nudge nudge'), 'wink wink') + def test_some_translations_with_context(self): + eq = self.assertEqual + # test some translations + eq(gettext.pgettext('my context', ur'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.pgettext('my context', 'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.pgettext(u'my context', 'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.pgettext('my other context', ur'nudge nudge'), + 'wink wink (in \"my other context\")') + eq(gettext.pgettext('my other context', 'nudge nudge'), + 'wink wink (in \"my other context\")') + eq(gettext.pgettext(u'my other context', 'nudge nudge'), + 'wink wink (in \"my other context\")') + + def test_some_translations_with_context_and_domain(self): + eq = self.assertEqual + # test some translations + eq(gettext.dpgettext('gettext', 'my context', ur'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.dpgettext('gettext', 'my context', 'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.dpgettext('gettext', u'my context', 'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.dpgettext('gettext', 'my other context', ur'nudge nudge'), + 'wink wink (in \"my other context\")') + eq(gettext.dpgettext('gettext', 'my other context', 'nudge nudge'), + 'wink wink (in \"my other context\")') + eq(gettext.dpgettext('gettext', u'my other context', 'nudge nudge'), + 'wink wink (in \"my other context\")') + def test_double_quotes(self): eq = self.assertEqual # double quotes @@ -214,6 +265,109 @@ 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): + unless = self.failUnless + eq = self.assertEqual + # test some translations + t = gettext.lgettext('nudge nudge') + unless(isinstance(t, str)) + eq(t, 'wink wink') + t = gettext.lgettext(u'nudge nudge') + unless(isinstance(t, str)) + eq(t, 'wink wink') + + def test_some_translations_with_context(self): + unless = self.failUnless + eq = self.assertEqual + # test some translations + t = gettext.lpgettext('my context', 'nudge nudge') + unless(isinstance(t, str)) + eq(t, 'wink wink (in "my context")') + t = gettext.lpgettext('my context', u'nudge nudge') + unless(isinstance(t, str)) + eq(t, 'wink wink (in "my context")') + + def test_some_plural_translations(self): + unless = self.failUnless + eq = self.assertEqual + # test some translations + t = gettext.lngettext(u'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', u'There are %s files', 2) + unless(isinstance(t, str)) + eq(t, 'Hay %s ficheros') + + def test_some_plural_translations_with_context(self): + unless = self.failUnless + eq = self.assertEqual + # test some translations + t = gettext.lnpgettext('With context', u'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', u'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 + # test some translations + eq(gettext.dgettext('gettext', 'nudge nudge'), 'wink wink') + eq(gettext.dgettext('gettext', 'nudge nudge'), 'wink wink') + eq(gettext.dgettext('gettext', ur'nudge nudge'), 'wink wink') + eq(gettext.dgettext('gettext', 'nudge nudge'), 'wink wink') + eq(gettext.dgettext('gettext', 'nudge nudge'), 'wink wink') + + def test_some_plural_translations_with_domain(self): + eq = self.assertEqual + # test some translations + 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 + # test some translations + eq(gettext.dpgettext('gettext', 'my context', ur'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.dpgettext('gettext', 'my context', 'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.dpgettext('gettext', u'my context', 'nudge nudge'), + 'wink wink (in \"my context\")') + eq(gettext.dpgettext('gettext', 'my other context', ur'nudge nudge'), + 'wink wink (in \"my other context\")') + eq(gettext.dpgettext('gettext', 'my other context', 'nudge nudge'), + 'wink wink (in \"my other context\")') + eq(gettext.dpgettext('gettext', u'my other context', 'nudge nudge'), + 'wink wink (in \"my other context\")') + + def test_some_plural_translations_with_domain_and_context(self): + eq = self.assertEqual + # test some translations + 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) @@ -226,6 +380,13 @@ 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 fp = open(self.mofile, 'rb') @@ -236,6 +397,16 @@ 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 + fp = open(self.mofile, 'rb') + t = gettext.GNUTranslations(fp) + fp.close() + 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') @@ -306,17 +477,83 @@ finally: fp.close() self._ = self.t.ugettext + self.upgettext = self.t.upgettext + self.unpgettext = self.t.unpgettext def test_unicode_msgid(self): unless = self.failUnless unless(isinstance(self._(''), unicode)) unless(isinstance(self._(u''), unicode)) + def test_unicode_context_msgid(self): + unless = self.failUnless + unless(isinstance(self.upgettext('', ''), unicode)) + unless(isinstance(self.upgettext(u'', u''), unicode)) + unless(isinstance(self.upgettext(u'', ''), unicode)) + unless(isinstance(self.upgettext('', u''), unicode)) + def test_unicode_msgstr(self): eq = self.assertEqual - eq(self._(u'ab\xde'), u'\xa4yz') + unless = self.failUnless + t = self._(u'ab\xde') + unless(isinstance(t, unicode)) + eq(t, u'\xa4yz') + def test_unicode_context_msgstr(self): + eq = self.assertEqual + unless = self.failUnless + t = self.upgettext(u"mycontext\xde", u'ab\xde') + unless(isinstance(t, unicode)) + eq(t, u'\xa4yz (context version)') + +class UnicodeTranslationsPluralTest(GettextBaseTest): + def setUp(self): + GettextBaseTest.setUp(self) + fp = open(MOFILE, 'rb') + try: + self.t = gettext.GNUTranslations(fp) + finally: + fp.close() + self.ungettext = self.t.ungettext + self.unpgettext = self.t.unpgettext + + def test_unicode_msgid(self): + unless = self.failUnless + unless(isinstance(self.ungettext('', '', 1), unicode)) + unless(isinstance(self.ungettext('', '', 2), unicode)) + unless(isinstance(self.ungettext(u'', u'', 1), unicode)) + unless(isinstance(self.ungettext(u'', u'', 2), unicode)) + + def test_unicode_context_msgid(self): + unless = self.failUnless + unless(isinstance(self.unpgettext('', '', '', 1), unicode)) + unless(isinstance(self.unpgettext('', '', '', 2), unicode)) + unless(isinstance(self.unpgettext(u'', u'', u'', 1), unicode)) + unless(isinstance(self.unpgettext(u'', u'', u'', 2), unicode)) + + def test_unicode_msgstr(self): + eq = self.assertEqual + unless = self.failUnless + t = self.ungettext(u"There is %s file", u"There are %s files", 1) + unless(isinstance(t, unicode)) + eq(t, u"Hay %s fichero") + unless(isinstance(t, unicode)) + t = self.ungettext(u"There is %s file", u"There are %s files", 5) + unless(isinstance(t, unicode)) + eq(t, u"Hay %s ficheros") + + def test_unicode_msgstr_with_context(self): + eq = self.assertEqual + unless = self.failUnless + t = self.unpgettext(u"With context", u"There is %s file", u"There are %s files", 1) + unless(isinstance(t, unicode)) + eq(t, u"Hay %s fichero (context)") + t = self.unpgettext(u"With context", u"There is %s file", u"There are %s files", 5) + unless(isinstance(t, unicode)) + eq(t, u"Hay %s ficheros (context)") + + class WeirdMetadataTest(GettextBaseTest): def setUp(self): GettextBaseTest.setUp(self) @@ -371,6 +608,14 @@ 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" @@ -403,6 +648,14 @@ 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 @@ -427,6 +680,11 @@ #: 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