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