translate.tools.pocompile

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 mo
    30  from translate.misc.multistring import multistring
    31
    32 -def _do_msgidcomment(string):
    33      return u"_: %s\n" % string
    34
    35 -class POCompile:
    36
    37 -    def convertstore(self, inputfile, includefuzzy=False):
    38          outputfile = mo.mofile()
    39          for unit in inputfile.units:
    40              if unit.istranslated() or (unit.isfuzzy() and includefuzzy and unit.target):
    41                  mounit = mo.mounit()
    42                  if unit.isheader():
    43                      mounit.source = ""
    44                  else:
    45                      mounit.source = unit.source
    46                      context = unit.getcontext()
    47                      if unit.msgidcomment:
    48                          if mounit.hasplural():
    49                              mounit.source = multistring(_do_msgidcomment(unit.msgidcomment) + mounit.source, *mounit.source.strings[1:])
    50                          else:
    51                              mounit.source = _do_msgidcomment(unit.msgidcomment) + mounit.source
    52                      elif context:
    53                          mounit.msgctxt = [context]
    54                  mounit.target = unit.target
    55                  outputfile.addunit(mounit)
    56          return str(outputfile)
    57
    58 -def convertmo(inputfile, outputfile, templatefile, includefuzzy=False):
    59      """reads in a base class derived inputfile, converts using pocompile, writes to outputfile"""
    60      # note that templatefile is not used, but it is required by the converter...
    61      inputstore = factory.getobject(inputfile)
    62      if inputstore.isempty():
    63          return 0
    64      convertor = POCompile()
    65      outputmo = convertor.convertstore(inputstore, includefuzzy)
    66      # We have to make sure that we write the files in binary mode, therefore we
    67      # reopen the file accordingly
    68      outputfile.close()
    69      outputfile = open(outputfile.name, 'wb')
    70      outputfile.write(outputmo)
    71      return 1
    72
    73 -def main():
    74      from translate.convert import convert
    75      formats = {"po":("mo", convertmo), "xlf":("mo", convertmo)}
    76      parser = convert.ConvertOptionParser(formats, usepots=False, description=__doc__)
    77      parser.add_fuzzy_option()
    78      parser.run()
    79
    80  if __name__ == '__main__':
    81      main()
    82