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