cubicweb/cwgettext.py
changeset 11057 0b59724cb3f2
parent 10719 065b5ac5c039
child 11823 4f43e64603ef
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    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/>.
       
    18 
       
    19 import gettext
       
    20 
       
    21 
       
    22 class cwGNUTranslations(gettext.GNUTranslations):
       
    23     # The encoding of a msgctxt and a msgid in a .mo file is
       
    24     # msgctxt + "\x04" + msgid (gettext version >= 0.15)
       
    25     CONTEXT_ENCODING = "%s\x04%s"
       
    26 
       
    27     def pgettext(self, context, message):
       
    28         ctxt_msg_id = self.CONTEXT_ENCODING % (context, message)
       
    29         missing = object()
       
    30         tmsg = self._catalog.get(ctxt_msg_id, missing)
       
    31         if tmsg is missing:
       
    32             if self._fallback:
       
    33                 return self._fallback.pgettext(context, message)
       
    34             return message
       
    35         # Encode the Unicode tmsg back to an 8-bit string, if possible
       
    36         if self._output_charset:
       
    37             return tmsg.encode(self._output_charset)
       
    38         elif self._charset:
       
    39             return tmsg.encode(self._charset)
       
    40         return tmsg
       
    41 
       
    42     def lpgettext(self, context, message):
       
    43         ctxt_msg_id = self.CONTEXT_ENCODING % (context, message)
       
    44         missing = object()
       
    45         tmsg = self._catalog.get(ctxt_msg_id, missing)
       
    46         if tmsg is missing:
       
    47             if self._fallback:
       
    48                 return self._fallback.lpgettext(context, message)
       
    49             return message
       
    50         if self._output_charset:
       
    51             return tmsg.encode(self._output_charset)
       
    52         return tmsg.encode(locale.getpreferredencoding())
       
    53 
       
    54     def npgettext(self, context, msgid1, msgid2, n):
       
    55         ctxt_msg_id = self.CONTEXT_ENCODING % (context, msgid1)
       
    56         try:
       
    57             tmsg = self._catalog[(ctxt_msg_id, self.plural(n))]
       
    58             if self._output_charset:
       
    59                 return tmsg.encode(self._output_charset)
       
    60             elif self._charset:
       
    61                 return tmsg.encode(self._charset)
       
    62             return tmsg
       
    63         except KeyError:
       
    64             if self._fallback:
       
    65                 return self._fallback.npgettext(context, msgid1, msgid2, n)
       
    66             if n == 1:
       
    67                 return msgid1
       
    68             else:
       
    69                 return msgid2        
       
    70 
       
    71     def lnpgettext(self, context, msgid1, msgid2, n):
       
    72         ctxt_msg_id = self.CONTEXT_ENCODING % (context, msgid1)
       
    73         try:
       
    74             tmsg = self._catalog[(ctxt_msg_id, self.plural(n))]
       
    75             if self._output_charset:
       
    76                 return tmsg.encode(self._output_charset)
       
    77             return tmsg.encode(locale.getpreferredencoding())
       
    78         except KeyError:
       
    79             if self._fallback:
       
    80                 return self._fallback.lnpgettext(context, msgid1, msgid2, n)
       
    81             if n == 1:
       
    82                 return msgid1
       
    83             else:
       
    84                 return msgid2
       
    85 
       
    86     def upgettext(self, context, message):
       
    87         ctxt_message_id = self.CONTEXT_ENCODING % (context, message)
       
    88         missing = object()
       
    89         tmsg = self._catalog.get(ctxt_message_id, missing)
       
    90         if tmsg is missing:
       
    91             # XXX logilab patch for compat w/ catalog generated by cw < 3.5
       
    92             return self.ugettext(message)
       
    93             if self._fallback:
       
    94                 return self._fallback.upgettext(context, message)
       
    95             return unicode(message)
       
    96         return tmsg
       
    97 
       
    98     def unpgettext(self, context, msgid1, msgid2, n):
       
    99         ctxt_message_id = self.CONTEXT_ENCODING % (context, msgid1)
       
   100         try:
       
   101             tmsg = self._catalog[(ctxt_message_id, self.plural(n))]
       
   102         except KeyError:
       
   103             if self._fallback:
       
   104                 return self._fallback.unpgettext(context, msgid1, msgid2, n)
       
   105             if n == 1:
       
   106                 tmsg = unicode(msgid1)
       
   107             else:
       
   108                 tmsg = unicode(msgid2)
       
   109         return tmsg
       
   110 
       
   111 
       
   112 def translation(domain, localedir=None, languages=None,
       
   113                 class_=None, fallback=False, codeset=None):
       
   114     if class_ is None:
       
   115         class_ = cwGNUTranslations
       
   116     return gettext.translation(domain, localedir=localedir,
       
   117                                languages=languages, class_=class_,
       
   118                                fallback=fallback, codeset=codeset)