summaryrefslogtreecommitdiff
path: root/babel/support.py
diff options
context:
space:
mode:
authorPedro Algarvio <pedro@algarvio.me>2008-12-18 00:14:40 +0000
committerPedro Algarvio <pedro@algarvio.me>2008-12-18 00:14:40 +0000
commitb80d451e7831dd8e077d287c0ca3663fb5b074d1 (patch)
treeaea922b6d8e846365fe31ea36454c7179e8ea1f7 /babel/support.py
parente79ee9435555b9ef8a4738b370ce1504226d2118 (diff)
downloadbabel-b80d451e7831dd8e077d287c0ca3663fb5b074d1.tar.gz
Add support for `msgctxt`. See #54.
Diffstat (limited to 'babel/support.py')
-rw-r--r--babel/support.py173
1 files changed, 173 insertions, 0 deletions
diff --git a/babel/support.py b/babel/support.py
index ddc0894..20eac91 100644
--- a/babel/support.py
+++ b/babel/support.py
@@ -19,6 +19,7 @@ in applications.
from datetime import date, datetime, time, timedelta
import gettext
+import locale
from babel.core import Locale
from babel.dates import format_date, format_datetime, format_time, \
@@ -404,3 +405,175 @@ class Translations(gettext.GNUTranslations, object):
domain.
"""
return self._domains.get(domain, self).ungettext(singular, plural, num)
+
+ # Most of the downwards code, until it get's included in stdlib, from:
+ # http://bugs.python.org/file10036/gettext-pgettext.patch
+ #
+ # 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 pgettext(self, 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 ``pgettext()``
+ method. Otherwise, the `message` id is returned.
+ """
+ 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 lpgettext(self, context, message):
+ """Equivalent to ``pgettext()``, but the translation is returned in the
+ preferred system encoding, if no other encoding was explicitly set with
+ ``bind_textdomain_codeset()``.
+ """
+ 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 npgettext(self, context, singular, plural, num):
+ """Do a plural-forms lookup of a message id. `singular` is used as the
+ message id for purposes of lookup in the catalog, while `num` 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
+ ``npgettext()`` method. Otherwise, when ``num`` is 1 ``singular`` is
+ returned, and ``plural`` is returned in all other cases.
+ """
+ ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
+ try:
+ tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
+ 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, singular, plural, num)
+ if num == 1:
+ return singular
+ else:
+ return plural
+
+ def lnpgettext(self, context, singular, plural, num):
+ """Equivalent to ``npgettext()``, but the translation is returned in the
+ preferred system encoding, if no other encoding was explicitly set with
+ ``bind_textdomain_codeset()``.
+ """
+ ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
+ try:
+ tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
+ 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, singular, plural, num)
+ if num == 1:
+ return singular
+ else:
+ return plural
+
+ def upgettext(self, 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 ``upgettext()``
+ method. Otherwise, the `message` id is returned.
+ """
+ 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 unpgettext(self, context, singular, plural, num):
+ """Do a plural-forms lookup of a message id. `singular` is used as the
+ message id for purposes of lookup in the catalog, while `num` 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
+ ``unpgettext()`` method. Otherwise, when `num` is 1 `singular` is
+ returned, and `plural` is returned in all other cases.
+ """
+ ctxt_message_id = self.CONTEXT_ENCODING % (context, singular)
+ try:
+ tmsg = self._catalog[(ctxt_message_id, self.plural(num))]
+ except KeyError:
+ if self._fallback:
+ return self._fallback.unpgettext(context, singular, plural, num)
+ if num == 1:
+ tmsg = unicode(singular)
+ else:
+ tmsg = unicode(plural)
+ return tmsg
+
+ def dpgettext(self, domain, context, message):
+ """Like `pgettext()`, but look the message up in the specified
+ `domain`.
+ """
+ return self._domains.get(domain, self).pgettext(context, message)
+
+ def dupgettext(self, domain, context, message):
+ """Like `upgettext()`, but look the message up in the specified
+ `domain`.
+ """
+ return self._domains.get(domain, self).upgettext(context, message)
+
+ def ldpgettext(self, domain, context, message):
+ """Equivalent to ``dpgettext()``, but the translation is returned in the
+ preferred system encoding, if no other encoding was explicitly set with
+ ``bind_textdomain_codeset()``.
+ """
+ return self._domains.get(domain, self).lpgettext(context, message)
+
+ def dnpgettext(self, domain, context, singular, plural, num):
+ """Like ``npgettext``, but look the message up in the specified
+ `domain`.
+ """
+ return self._domains.get(domain, self).npgettext(context, singular,
+ plural, num)
+
+ def dunpgettext(self, domain, context, singular, plural, num):
+ """Like ``unpgettext``, but look the message up in the specified
+ `domain`.
+ """
+ return self._domains.get(domain, self).unpgettext(context, singular,
+ plural, num)
+
+ def ldnpgettext(self, domain, context, singular, plural, num):
+ """Equivalent to ``dnpgettext()``, but the translation is returned in
+ the preferred system encoding, if no other encoding was explicitly set
+ with ``bind_textdomain_codeset()``.
+ """
+ return self._domains.get(domain, self).lnpgettext(context, singular,
+ plural, num)
+