translate.convert.po2ts

Package translate :: Package convert :: Module po2ts |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2ts

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2004-2006 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     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
    23  """convert Gettext PO localization files to Qt Linguist (.ts) files
    24
    25  see: http://translate.sourceforge.net/wiki/toolkit/po2ts for examples and
    26  usage instructions
    27  """
    28
    29  from translate.storage import po
    30  from translate.storage import ts
    31
    32 -class po2ts:
    33 -    def convertstore(self, inputstore, templatefile=None, context=None):
    34          """converts a .po file to .ts format (using a template .ts file if given)"""
    35          if templatefile is None:
    36              tsfile = ts.QtTsParser()
    37          else:
    38              tsfile = ts.QtTsParser(templatefile)
    39          for inputunit in inputstore.units:
    40              if inputunit.isheader() or inputunit.isblank():
    41                  continue
    42              source = inputunit.source
    43              translation = inputunit.target
    44              comment = inputunit.getnotes("translator")
    45              transtype = None
    46              if not inputunit.istranslated():
    47                  transtype = "unfinished"
    48              elif inputunit.getnotes("developer") == "(obsolete)":
    49                  transtype = "obsolete"
    50              if isinstance(source, str):
    51                  source = source.decode("utf-8")
    52              if isinstance(translation, str):
    53                  translation = translation.decode("utf-8")
    54              for sourcelocation in inputunit.getlocations():
    55                  if context is None:
    56                      if "#" in sourcelocation:
    57                          contextname = sourcelocation[:sourcelocation.find("#")]
    58                      else:
    59                          contextname = sourcelocation
    60                  else:
    61                      contextname = context
    62                  tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True)
    63          return tsfile.getxml()
    64
    65 -def convertpo(inputfile, outputfile, templatefile, context):
    66      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
    67      inputstore = po.pofile(inputfile)
    68      if inputstore.isempty():
    69          return 0
    70      convertor = po2ts()
    71      outputstring = convertor.convertstore(inputstore, templatefile, context)
    72      outputfile.write(outputstring)
    73      return 1
    74
    75 -def main(argv=None):
    76      from translate.convert import convert
    77      formats = {"po": ("ts", convertpo), ("po", "ts"): ("ts", convertpo)}
    78      parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__)
    79      parser.add_option("-c", "--context", dest="context", default=None,
    80                        help="use supplied context instead of the one in the .po file comment")
    81      parser.passthrough.append("context")
    82      parser.run(argv)
    83
    84
    85  if __name__ == '__main__':
    86      main()
    87