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
    33 -class po2ts:
    34
    35 -    def convertstore(self, inputstore, templatefile=None, context=None):
    36          """converts a .po file to .ts format (using a template .ts file if given)"""
    37          if templatefile is None:
    38              tsfile = ts.QtTsParser()
    39          else:
    40              tsfile = ts.QtTsParser(templatefile)
    41          for inputunit in inputstore.units:
    42              if inputunit.isheader() or inputunit.isblank():
    43                  continue
    44              source = inputunit.source
    45              translation = inputunit.target
    46              comment = inputunit.getnotes("translator")
    47              transtype = None
    48              if not inputunit.istranslated():
    49                  transtype = "unfinished"
    50              elif inputunit.getnotes("developer") == "(obsolete)":
    51                  transtype = "obsolete"
    52              if isinstance(source, str):
    53                  source = source.decode("utf-8")
    54              if isinstance(translation, str):
    55                  translation = translation.decode("utf-8")
    56              for sourcelocation in inputunit.getlocations():
    57                  if context is None:
    58                      if "#" in sourcelocation:
    59                          contextname = sourcelocation[:sourcelocation.find("#")]
    60                      else:
    61                          contextname = sourcelocation
    62                  else:
    63                      contextname = context
    64                  tsfile.addtranslation(contextname, source, translation, comment, transtype, createifmissing=True)
    65          return tsfile.getxml()
    66
    67
    68 -def convertpo(inputfile, outputfile, templatefile, context):
    69      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
    70      inputstore = po.pofile(inputfile)
    71      if inputstore.isempty():
    72          return 0
    73      convertor = po2ts()
    74      outputstring = convertor.convertstore(inputstore, templatefile, context)
    75      outputfile.write(outputstring)
    76      return 1
    77
    78
    79 -def main(argv=None):
    80      from translate.convert import convert
    81      formats = {"po": ("ts", convertpo), ("po", "ts"): ("ts", convertpo)}
    82      parser = convert.ConvertOptionParser(formats, usepots=False, usetemplates=True, description=__doc__)
    83      parser.add_option("-c", "--context", dest="context", default=None,
    84                        help="use supplied context instead of the one in the .po file comment")
    85      parser.passthrough.append("context")
    86      parser.run(argv)
    87
    88
    89  if __name__ == '__main__':
    90      main()
    91