Package translate :: Package tools :: Module pocompile |
[hide private]
[frames] | no frames]

# Source Code for Module translate.tools.pocompile

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2005, 2006 Zuza Software Foundation
     5  #
     6  # This file is part of the translate-toolkit
     7  #
     8  # translate is free software; you can redistribute it and/or modify
     9  # it under the terms of the GNU General Public License as published by
    10  # the Free Software Foundation; either version 2 of the License, or
    11  # (at your option) any later version.
    12  #
    13  # translate is distributed in the hope that it will be useful,
    14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  # GNU General Public License for more details.
    17  #
    18  # You should have received a copy of the GNU General Public License
    19  # along with translate; if not, write to the Free Software
    20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21
    22  """Compile XLIFF and Gettext PO localization files into Gettext MO (Machine Object) files
    23
    24  See: http://translate.sourceforge.net/wiki/toolkit/pocompile for examples and
    25  usage instructions
    26  """
    27
    28  from translate.storage import factory
    29  from translate.storage import po
    30  from translate.storage import mo
    31
    32 -class POCompile:
    33
    34 -    def convertstore(self, inputfile, includefuzzy=False):
    35          outputfile = mo.mofile()
    36          for unit in inputfile.units:
    37              if unit.istranslated() or (unit.isfuzzy() and includefuzzy and unit.target):
    38                  mounit = mo.mounit()
    39                  if unit.isheader():
    40                      mounit.source = ""
    41                  else:
    42                      mounit.source = unit.source
    43                      if hasattr(unit, "msgidcomments"):
    44                          mounit.source.strings[0] = po.unquotefrompo(unit.msgidcomments) + mounit.source.strings[0]
    45                      if hasattr(unit, "msgctxt"):
    46                          mounit.msgctxt = po.unquotefrompo(unit.msgctxt)
    47                  mounit.target = unit.target
    48                  outputfile.addunit(mounit)
    49          return str(outputfile)
    50
    51 -def convertmo(inputfile, outputfile, templatefile, includefuzzy=False):
    52      """reads in a base class derived inputfile, converts using pocompile, writes to outputfile"""
    53      # note that templatefile is not used, but it is required by the converter...
    54      inputstore = factory.getobject(inputfile)
    55      if inputstore.isempty():
    56          return 0
    57      convertor = POCompile()
    58      outputmo = convertor.convertstore(inputstore, includefuzzy)
    59      # We have to make sure that we write the files in binary mode, therefore we
    60      # reopen the file accordingly
    61      outputfile.close()
    62      outputfile = open(outputfile.name, 'wb')
    63      outputfile.write(outputmo)
    64      return 1
    65
    66 -def main():
    67      from translate.convert import convert
    68      formats = {"po":("mo", convertmo), "xlf":("mo", convertmo)}
    69      parser = convert.ConvertOptionParser(formats, usepots=False, description=__doc__)
    70      parser.add_fuzzy_option()
    71      parser.run()
    72
    73  if __name__ == '__main__':
    74      main()
    75