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