Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes
MoinMoin.i18n.msgfmt.MsgFmt Class Reference

List of all members.

Public Member Functions

def __init__
def make_filenames
def add
def read_po
def generate_mo

Public Attributes

 messages

Detailed Description

transform .po -> .mo format

Definition at line 38 of file msgfmt.py.


Constructor & Destructor Documentation

Definition at line 40 of file msgfmt.py.

00040 
00041     def __init__(self):
00042         self.messages = {}


Member Function Documentation

def MoinMoin.i18n.msgfmt.MsgFmt.add (   self,
  id,
  str,
  fuzzy 
)
Add a non-fuzzy translation to the dictionary.

Definition at line 53 of file msgfmt.py.

00053 
00054     def add(self, id, str, fuzzy):
00055         """Add a non-fuzzy translation to the dictionary."""
00056         if not fuzzy and str:
00057             self.messages[id] = str

Here is the caller graph for this function:

Return the generated output.

Definition at line 107 of file msgfmt.py.

00107 
00108     def generate_mo(self):
00109         """Return the generated output."""
00110         keys = self.messages.keys()
00111         # the keys are sorted in the .mo file
00112         keys.sort()
00113         offsets = []
00114         ids = ''
00115         strs = ''
00116         for id in keys:
00117             # For each string, we need size and file offset.  Each string is NUL
00118             # terminated; the NUL does not count into the size.
00119             offsets.append((len(ids), len(id), len(strs), len(self.messages[id])))
00120             ids += id + '\0'
00121             strs += self.messages[id] + '\0'
00122         output = []
00123         # The header is 7 32-bit unsigned integers.  We don't use hash tables, so
00124         # the keys start right after the index tables.
00125         # translated string.
00126         keystart = 7*4 + 16*len(keys)
00127         # and the values start after the keys
00128         valuestart = keystart + len(ids)
00129         koffsets = []
00130         voffsets = []
00131         # The string table first has the list of keys, then the list of values.
00132         # Each entry has first the size of the string, then the file offset.
00133         for o1, l1, o2, l2 in offsets:
00134             koffsets += [l1, o1 + keystart]
00135             voffsets += [l2, o2 + valuestart]
00136         offsets = koffsets + voffsets
00137         output.append(struct.pack("Iiiiiii",
00138                              0x950412deL,       # Magic
00139                              0,                 # Version
00140                              len(keys),         # # of entries
00141                              7*4,               # start of key index
00142                              7*4 + len(keys)*8, # start of value index
00143                              0, 0))             # size and offset of hash table
00144         output.append(array.array("i", offsets).tostring())
00145         output.append(ids)
00146         output.append(strs)
00147         return ''.join(output)
00148 

def MoinMoin.i18n.msgfmt.MsgFmt.make_filenames (   self,
  filename,
  outfile = None 
)
Compute .mo name from .po name or language

Definition at line 43 of file msgfmt.py.

00043 
00044     def make_filenames(self, filename, outfile=None):
00045         """Compute .mo name from .po name or language"""
00046         if filename.endswith('.po'):
00047             infile = filename
00048         else:
00049             infile = filename + '.po'
00050         if outfile is None:
00051             outfile = os.path.splitext(infile)[0] + '.mo'
00052         return infile, outfile

def MoinMoin.i18n.msgfmt.MsgFmt.read_po (   self,
  lines 
)

Definition at line 58 of file msgfmt.py.

00058 
00059     def read_po(self, lines):
00060         ID = 1
00061         STR = 2
00062         section = None
00063         fuzzy = False
00064         line_no = 0
00065         msgid = msgstr = ''
00066         # Parse the catalog
00067         for line in lines:
00068             line_no += 1
00069             # If we get a comment line after a msgstr, this is a new entry
00070             if line.startswith('#') and section == STR:
00071                 self.add(msgid, msgstr, fuzzy)
00072                 section = None
00073                 fuzzy = False
00074             # Record a fuzzy mark
00075             if line.startswith('#,') and 'fuzzy' in line:
00076                 fuzzy = True
00077             # Skip comments
00078             if line.startswith('#'):
00079                 continue
00080             # Now we are in a msgid section, output previous section
00081             if line.startswith('msgid'):
00082                 if section == STR:
00083                     self.add(msgid, msgstr, fuzzy)
00084                     fuzzy = False
00085                 section = ID
00086                 line = line[5:]
00087                 msgid = msgstr = ''
00088             # Now we are in a msgstr section
00089             elif line.startswith('msgstr'):
00090                 section = STR
00091                 line = line[6:]
00092             # Skip empty lines
00093             line = line.strip()
00094             if not line:
00095                 continue
00096             # XXX: Does this always follow Python escape semantics?
00097             line = eval(line)
00098             if section == ID:
00099                 msgid += line
00100             elif section == STR:
00101                 msgstr += line
00102             else:
00103                 raise SyntaxErrorException('Syntax error on line %d, before:\n%s' % (line_no, line))
00104         # Add last entry
00105         if section == STR:
00106             self.add(msgid, msgstr, fuzzy)

Here is the call graph for this function:


Member Data Documentation

Definition at line 41 of file msgfmt.py.


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