cubicweb/cwgettext.py
changeset 12567 26744ad37953
parent 11823 4f43e64603ef
equal deleted inserted replaced
12566:6b3523f81f42 12567:26744ad37953
    15 #
    15 #
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 
    18 
    19 import gettext
    19 import gettext
    20 
       
    21 from six import PY3
       
    22 
    20 
    23 
    21 
    24 class cwGNUTranslations(gettext.GNUTranslations):
    22 class cwGNUTranslations(gettext.GNUTranslations):
    25     # The encoding of a msgctxt and a msgid in a .mo file is
    23     # The encoding of a msgctxt and a msgid in a .mo file is
    26     # msgctxt + "\x04" + msgid (gettext version >= 0.15)
    24     # msgctxt + "\x04" + msgid (gettext version >= 0.15)
    83             if n == 1:
    81             if n == 1:
    84                 return msgid1
    82                 return msgid1
    85             else:
    83             else:
    86                 return msgid2
    84                 return msgid2
    87 
    85 
    88     if PY3:
    86     ugettext = gettext.GNUTranslations.gettext
    89         ugettext = gettext.GNUTranslations.gettext
       
    90 
    87 
    91     def upgettext(self, context, message):
    88     def upgettext(self, context, message):
    92         ctxt_message_id = self.CONTEXT_ENCODING % (context, message)
    89         ctxt_message_id = self.CONTEXT_ENCODING % (context, message)
    93         missing = object()
    90         missing = object()
    94         tmsg = self._catalog.get(ctxt_message_id, missing)
    91         tmsg = self._catalog.get(ctxt_message_id, missing)
    95         if tmsg is missing:
    92         if tmsg is missing:
    96             # XXX logilab patch for compat w/ catalog generated by cw < 3.5
    93             # XXX logilab patch for compat w/ catalog generated by cw < 3.5
    97             return self.ugettext(message)
    94             return self.ugettext(message)
    98             if self._fallback:
    95             if self._fallback:
    99                 return self._fallback.upgettext(context, message)
    96                 return self._fallback.upgettext(context, message)
   100             return unicode(message)
    97             return str(message)
   101         return tmsg
    98         return tmsg
   102 
    99 
   103     def unpgettext(self, context, msgid1, msgid2, n):
   100     def unpgettext(self, context, msgid1, msgid2, n):
   104         ctxt_message_id = self.CONTEXT_ENCODING % (context, msgid1)
   101         ctxt_message_id = self.CONTEXT_ENCODING % (context, msgid1)
   105         try:
   102         try:
   106             tmsg = self._catalog[(ctxt_message_id, self.plural(n))]
   103             tmsg = self._catalog[(ctxt_message_id, self.plural(n))]
   107         except KeyError:
   104         except KeyError:
   108             if self._fallback:
   105             if self._fallback:
   109                 return self._fallback.unpgettext(context, msgid1, msgid2, n)
   106                 return self._fallback.unpgettext(context, msgid1, msgid2, n)
   110             if n == 1:
   107             if n == 1:
   111                 tmsg = unicode(msgid1)
   108                 tmsg = str(msgid1)
   112             else:
   109             else:
   113                 tmsg = unicode(msgid2)
   110                 tmsg = str(msgid2)
   114         return tmsg
   111         return tmsg
   115 
   112 
   116 
   113 
   117 def translation(domain, localedir=None, languages=None,
   114 def translation(domain, localedir=None, languages=None,
   118                 class_=None, fallback=False, codeset=None):
   115                 class_=None, fallback=False, codeset=None):