Example #1
0
    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 text_to_native(tmsg, self._output_charset)
            elif self._charset:
                return text_to_native(tmsg, 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
Example #2
0
 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 text_to_native(tmsg, self._output_charset)
     elif self._charset:
         return text_to_native(tmsg, self._charset)
     return tmsg