Package translate :: Package convert :: Module ts2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.ts2po

     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 Qt Linguist (.ts) files to Gettext PO localization files
    24
    25  See: http://translate.sourceforge.net/wiki/toolkit/ts2po for examples and
    26  usage instructions
    27  """
    28
    29
    30  from translate.storage import po
    31  from translate.storage import ts
    32
    33
    34 -class ts2po:
    35
    36 -    def __init__(self, duplicatestyle="msgctxt"):
    37          self.duplicatestyle = duplicatestyle
    38
    39 -    def convertmessage(self, contextname, messagenum, source, target, msgcomments, transtype):
    40          """makes a pounit from the given message"""
    41          thepo = po.pounit(encoding="UTF-8")
    42          thepo.addlocation("%s#%d" % (contextname, messagenum))
    43          thepo.source = source
    44          thepo.target = target
    45          if len(msgcomments) > 0:
    46              thepo.addnote(msgcomments)
    47          if transtype == "unfinished" and thepo.istranslated():
    48              thepo.markfuzzy()
    49          if transtype == "obsolete":
    50              # This should use the Gettext obsolete method but it would require quite a bit of work
    51              thepo.addnote("(obsolete)", origin="developer")
    52              # using the fact that -- quote -- "(this is nonsense)"
    53          return thepo
    54
    55 -    def convertfile(self, inputfile):
    56          """converts a .ts file to .po format"""
    57          tsfile = ts.QtTsParser(inputfile)
    58          thetargetfile = po.pofile()
    59          targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
    60
    61          for contextname, messages in tsfile.iteritems():
    62              messagenum = 0
    63              for message in messages:
    64                  messagenum += 1
    65                  source = tsfile.getmessagesource(message)
    66                  translation = tsfile.getmessagetranslation(message)
    67                  comment = tsfile.getmessagecomment(message)
    68                  transtype = tsfile.getmessagetype(message)
    69                  thepo = self.convertmessage(contextname, messagenum, source, translation, comment, transtype)
    70                  thetargetfile.addunit(thepo)
    71          thetargetfile.removeduplicates(self.duplicatestyle)
    72          return thetargetfile
    73
    74
    75 -def convertts(inputfile, outputfile, templates, duplicatestyle="msgctxt"):
    76      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
    77      convertor = ts2po(duplicatestyle=duplicatestyle)
    78      outputstore = convertor.convertfile(inputfile)
    79      if outputstore.isempty():
    80          return 0
    81      outputfile.write(str(outputstore))
    82      return 1
    83
    84
    85 -def main(argv=None):
    86      from translate.convert import convert
    87      formats = {"ts": ("po", convertts)}
    88      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
    89      parser.add_duplicates_option()
    90      parser.run(argv)
    91