Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
gettext.GNUTranslations Class Reference
Inheritance diagram for gettext.GNUTranslations:
Inheritance graph
[legend]
Collaboration diagram for gettext.GNUTranslations:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def lgettext
def lngettext
def gettext
def ngettext
def add_fallback
def info
def charset
def output_charset
def set_output_charset
def install

Public Attributes

 plural

Static Public Attributes

int LE_MAGIC = 0x950412de
int BE_MAGIC = 0xde120495

Private Member Functions

def _parse

Private Attributes

 _catalog
 _charset

Detailed Description

Definition at line 223 of file gettext.py.


Member Function Documentation

def gettext.GNUTranslations._parse (   self,
  fp 
) [private]
Override this method to support alternative .mo formats.

Reimplemented from gettext.NullTranslations.

Definition at line 228 of file gettext.py.

00228 
00229     def _parse(self, fp):
00230         """Override this method to support alternative .mo formats."""
00231         unpack = struct.unpack
00232         filename = getattr(fp, 'name', '')
00233         # Parse the .mo file header, which consists of 5 little endian 32
00234         # bit words.
00235         self._catalog = catalog = {}
00236         self.plural = lambda n: int(n != 1) # germanic plural by default
00237         buf = fp.read()
00238         buflen = len(buf)
00239         # Are we big endian or little endian?
00240         magic = unpack('<I', buf[:4])[0]
00241         if magic == self.LE_MAGIC:
00242             version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
00243             ii = '<II'
00244         elif magic == self.BE_MAGIC:
00245             version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
00246             ii = '>II'
00247         else:
00248             raise IOError(0, 'Bad magic number', filename)
00249         # Now put all messages from the .mo file buffer into the catalog
00250         # dictionary.
00251         for i in range(0, msgcount):
00252             mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
00253             mend = moff + mlen
00254             tlen, toff = unpack(ii, buf[transidx:transidx+8])
00255             tend = toff + tlen
00256             if mend < buflen and tend < buflen:
00257                 msg = buf[moff:mend]
00258                 tmsg = buf[toff:tend]
00259             else:
00260                 raise IOError(0, 'File is corrupt', filename)
00261             # See if we're looking at GNU .mo conventions for metadata
00262             if mlen == 0:
00263                 # Catalog description
00264                 lastk = k = None
00265                 for b_item in tmsg.split('\n'.encode("ascii")):
00266                     item = b_item.decode().strip()
00267                     if not item:
00268                         continue
00269                     if ':' in item:
00270                         k, v = item.split(':', 1)
00271                         k = k.strip().lower()
00272                         v = v.strip()
00273                         self._info[k] = v
00274                         lastk = k
00275                     elif lastk:
00276                         self._info[lastk] += '\n' + item
00277                     if k == 'content-type':
00278                         self._charset = v.split('charset=')[1]
00279                     elif k == 'plural-forms':
00280                         v = v.split(';')
00281                         plural = v[1].split('plural=')[1]
00282                         self.plural = c2py(plural)
00283             # Note: we unconditionally convert both msgids and msgstrs to
00284             # Unicode using the character encoding specified in the charset
00285             # parameter of the Content-Type header.  The gettext documentation
00286             # strongly encourages msgids to be us-ascii, but some applications
00287             # require alternative encodings (e.g. Zope's ZCML and ZPT).  For
00288             # traditional gettext applications, the msgid conversion will
00289             # cause no problems since us-ascii should always be a subset of
00290             # the charset encoding.  We may want to fall back to 8-bit msgids
00291             # if the Unicode conversion fails.
00292             charset = self._charset or 'ascii'
00293             if b'\x00' in msg:
00294                 # Plural forms
00295                 msgid1, msgid2 = msg.split(b'\x00')
00296                 tmsg = tmsg.split(b'\x00')
00297                 msgid1 = str(msgid1, charset)
00298                 for i, x in enumerate(tmsg):
00299                     catalog[(msgid1, i)] = str(x, charset)
00300             else:
00301                 catalog[str(msg, charset)] = str(tmsg, charset)
00302             # advance to next entry in the seek tables
00303             masteridx += 8
00304             transidx += 8

Here is the call graph for this function:

def gettext.NullTranslations.add_fallback (   self,
  fallback 
) [inherited]

Definition at line 165 of file gettext.py.

00165 
00166     def add_fallback(self, fallback):
00167         if self._fallback:
00168             self._fallback.add_fallback(fallback)
00169         else:
00170             self._fallback = fallback

def gettext.NullTranslations.charset (   self) [inherited]

Definition at line 200 of file gettext.py.

00200 
00201     def charset(self):
00202         return self._charset

def gettext.GNUTranslations.gettext (   self,
  message 
)

Reimplemented from gettext.NullTranslations.

Definition at line 330 of file gettext.py.

00330 
00331     def gettext(self, message):
00332         missing = object()
00333         tmsg = self._catalog.get(message, missing)
00334         if tmsg is missing:
00335             if self._fallback:
00336                 return self._fallback.gettext(message)
00337             return message
00338         return tmsg

def gettext.NullTranslations.info (   self) [inherited]

Definition at line 197 of file gettext.py.

00197 
00198     def info(self):
00199         return self._info

Here is the caller graph for this function:

def gettext.NullTranslations.install (   self,
  names = None 
) [inherited]

Definition at line 209 of file gettext.py.

00209 
00210     def install(self, names=None):
00211         import builtins
00212         builtins.__dict__['_'] = self.gettext
00213         if hasattr(names, "__contains__"):
00214             if "gettext" in names:
00215                 builtins.__dict__['gettext'] = builtins.__dict__['_']
00216             if "ngettext" in names:
00217                 builtins.__dict__['ngettext'] = self.ngettext
00218             if "lgettext" in names:
00219                 builtins.__dict__['lgettext'] = self.lgettext
00220             if "lngettext" in names:
00221                 builtins.__dict__['lngettext'] = self.lngettext
00222 

Here is the call graph for this function:

def gettext.GNUTranslations.lgettext (   self,
  message 
)

Reimplemented from gettext.NullTranslations.

Definition at line 305 of file gettext.py.

00305 
00306     def lgettext(self, message):
00307         missing = object()
00308         tmsg = self._catalog.get(message, missing)
00309         if tmsg is missing:
00310             if self._fallback:
00311                 return self._fallback.lgettext(message)
00312             return message
00313         if self._output_charset:
00314             return tmsg.encode(self._output_charset)
00315         return tmsg.encode(locale.getpreferredencoding())

Here is the call graph for this function:

def gettext.GNUTranslations.lngettext (   self,
  msgid1,
  msgid2,
  n 
)

Reimplemented from gettext.NullTranslations.

Definition at line 316 of file gettext.py.

00316 
00317     def lngettext(self, msgid1, msgid2, n):
00318         try:
00319             tmsg = self._catalog[(msgid1, self.plural(n))]
00320             if self._output_charset:
00321                 return tmsg.encode(self._output_charset)
00322             return tmsg.encode(locale.getpreferredencoding())
00323         except KeyError:
00324             if self._fallback:
00325                 return self._fallback.lngettext(msgid1, msgid2, n)
00326             if n == 1:
00327                 return msgid1
00328             else:
00329                 return msgid2

Here is the call graph for this function:

def gettext.GNUTranslations.ngettext (   self,
  msgid1,
  msgid2,
  n 
)

Reimplemented from gettext.NullTranslations.

Definition at line 339 of file gettext.py.

00339 
00340     def ngettext(self, msgid1, msgid2, n):
00341         try:
00342             tmsg = self._catalog[(msgid1, self.plural(n))]
00343         except KeyError:
00344             if self._fallback:
00345                 return self._fallback.ngettext(msgid1, msgid2, n)
00346             if n == 1:
00347                 tmsg = msgid1
00348             else:
00349                 tmsg = msgid2
00350         return tmsg
00351 
00352 
# Locate a .mo file using the gettext strategy
def gettext.NullTranslations.output_charset (   self) [inherited]

Definition at line 203 of file gettext.py.

00203 
00204     def output_charset(self):
00205         return self._output_charset

def gettext.NullTranslations.set_output_charset (   self,
  charset 
) [inherited]

Definition at line 206 of file gettext.py.

00206 
00207     def set_output_charset(self, charset):
00208         self._output_charset = charset


Member Data Documentation

Definition at line 234 of file gettext.py.

Reimplemented from gettext.NullTranslations.

Definition at line 277 of file gettext.py.

int gettext.GNUTranslations.BE_MAGIC = 0xde120495 [static]

Definition at line 226 of file gettext.py.

int gettext.GNUTranslations.LE_MAGIC = 0x950412de [static]

Definition at line 225 of file gettext.py.

Definition at line 235 of file gettext.py.


The documentation for this class was generated from the following file: