translate.convert.po2tiki

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