Package translate :: Package convert :: Module tiki2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.tiki2po

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2008 Mozilla Corporation, 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  """ Convert TikiWiki's language.php files to GetText PO files. """
    23
    24  import sys
    25  from translate.storage import tiki
    26  from translate.storage import po
    27
    28 -class tiki2po:
    29 -    def __init__(self, includeunused=False):
    30          """
    31          @param includeunused: On conversion, should the "unused" section be preserved?  Default: False
    32          """
    33          self.includeunused = includeunused
    34
    35 -    def convertstore(self, thetikifile):
    36          """Converts a given (parsed) tiki file to a po file.
    37
    38          @param thetikifile: a tikifile pre-loaded with input data
    39          """
    40          thetargetfile = po.pofile()
    41
    42          # Set up the header
    43          targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
    44
    45          # For each lang unit, make the new po unit accordingly
    46          for unit in thetikifile.units:
    47              if not self.includeunused and "unused" in unit.getlocations():
    48                  continue
    49              newunit = po.pounit()
    50              newunit.source = unit.source
    51              newunit.settarget(unit.target)
    52              locations = unit.getlocations()
    53              if locations:
    54                  newunit.addlocations(locations)
    55              thetargetfile.addunit(newunit)
    56          return thetargetfile
    57
    58 -def converttiki(inputfile, outputfile, template=None, includeunused=False):
    59      """Converts from tiki file format to po.
    60
    61      @param inputfile: file handle of the source
    62      @param outputfile: file handle to write to
    63      @param template: unused
    64      @param includeunused: Include the "usused" section of the tiki file? Default: False
    65      """
    66      convertor = tiki2po(includeunused=includeunused)
    67      inputstore = tiki.TikiStore(inputfile)
    68      outputstore = convertor.convertstore(inputstore)
    69      if outputstore.isempty():
    70          return False
    71      outputfile.write(str(outputstore))
    72      return True
    73
    74 -def main(argv=None):
    75      """Converts tiki .php files to .po."""
    76      from translate.convert import convert
    77      from translate.misc import stdiotell
    78      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    79
    80      formats = {"php":("po",converttiki)}
    81
    82      parser = convert.ConvertOptionParser(formats, description=__doc__)
    83      parser.add_option("", "--include-unused", dest="includeunused", action="store_true", default=False, help="Include strings in the unused section")
    84      parser.passthrough.append("includeunused")
    85      parser.run(argv)
    86
    87  if __name__ == '__main__':
    88      main()
    89