Package translate :: Package convert :: Module po2tiki

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2tiki

     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 .po files to TikiWiki's language.php files. """
    23
    24  import sys
    25  from translate.storage import tiki
    26  from translate.storage import po
    27
    28 -class po2tiki:
    29 -    def convertstore(self, thepofile):
    30          """Converts a given (parsed) po file to a tiki file.
    31
    32          @param thepofile: a pofile pre-loaded with input data
    33          """
    34          thetargetfile = tiki.TikiStore()
    35          for unit in thepofile.units:
    36              if not (unit.isblank() or unit.isheader()):
    37                  newunit = tiki.TikiUnit(unit.source)
    38                  newunit.settarget(unit.target)
    39                  locations = unit.getlocations()
    40                  if locations:
    41                      newunit.addlocations(locations)
    42                  # If a word is "untranslated" but the target isn't empty and isn't the same as the source
    43                  # it's been translated and we switch it. This is an assumption but should remain true as long
    44                  # as these scripts are used.
    45                  if newunit.getlocations() == ["untranslated"] and unit.source != unit.target and unit.target != "":
    46                      newunit.location = []
    47                      newunit.addlocation("translated")
    48
    49                  thetargetfile.addunit(newunit)
    50          return thetargetfile
    51
    52 -def convertpo(inputfile, outputfile, template=None):
    53      """Converts from po file format to tiki.
    54
    55      @param inputfile: file handle of the source
    56      @param outputfile: file handle to write to
    57      @param template: unused
    58      """
    59      inputstore = po.pofile(inputfile)
    60      if inputstore.isempty():
    61          return False
    62      convertor = po2tiki()
    63      outputstore = convertor.convertstore(inputstore)
    64      outputfile.write(str(outputstore))
    65      return True
    66
    67 -def main(argv=None):
    68      """Will convert from .po to tiki style .php"""
    69      from translate.convert import convert
    70      from translate.misc import stdiotell
    71      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    72
    73      formats = {"po":("tiki",convertpo)}
    74
    75      parser = convert.ConvertOptionParser(formats, description=__doc__)
    76      parser.run(argv)
    77
    78  if __name__ == '__main__':
    79      main()
    80