Package translate :: Package convert :: Module php2po |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.php2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2002-2006 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 PHP localization files to Gettext PO localization files
     23
     24  See: http://translate.sourceforge.net/wiki/toolkit/php2po for examples and
     25  usage instructions
     26  """
     27
     28  import sys
     29  from translate.storage import po
     30  from translate.storage import php
     31
     32 -class php2po:
     33      """convert a .php file to a .po file for handling the translation..."""
     34 -    def convertstore(self, inputstore, duplicatestyle="msgctxt"):
     35          """converts a .php file to a .po file..."""
     36          outputstore = po.pofile()
     37          outputheader = outputstore.makeheader(charset="UTF-8", encoding="8bit")
     38          outputheader.addnote("extracted from %s" % inputstore.filename, "developer")
     39          outputstore.addunit(outputheader)
     40          for inputunit in inputstore.units:
     41              outputunit = self.convertunit(inputunit, "developer")
     42              if outputunit is not None:
     43                  outputstore.addunit(outputunit)
     44          outputstore.removeduplicates(duplicatestyle)
     45          return outputstore
     46
     47 -    def mergestore(self, templatestore, inputstore, blankmsgstr=False, duplicatestyle="msgctxt"):
     48          """converts two .properties files to a .po file..."""
     49          outputstore = po.pofile()
     50          outputheader = outputstore.makeheader(charset="UTF-8", encoding="8bit")
     51          outputheader.addnote("extracted from %s, %s" % (templatestore.filename, inputstore.filename), "developer")
     52          outputstore.addunit(outputheader)
     53          inputstore.makeindex()
     54          # loop through the original file, looking at units one by one
     55          for templateunit in templatestore.units:
     56              outputunit = self.convertunit(templateunit, "developer")
     57              # try and find a translation of the same name...
     58              if templateunit.name in inputstore.locationindex:
     59                  translatedinputunit = inputstore.locationindex[templateunit.name]
     60                  # Need to check that this comment is not a copy of the developer comments
     61                  translatedoutputunit = self.convertunit(translatedinputunit, "translator")
     62              else:
     63                  translatedoutputunit = None
     64              # if we have a valid po unit, get the translation and add it...
     65              if outputunit is not None:
     66                  if translatedoutputunit is not None and not blankmsgstr:
     67                      outputunit.target = translatedoutputunit.source
     68                  outputstore.addunit(outputunit)
     69              elif translatedoutputunit is not None:
     70                  print >> sys.stderr, "error converting original properties definition %s" % templateunit.name
     71          outputstore.removeduplicates(duplicatestyle)
     72          return outputstore
     73
     74 -    def convertunit(self, inputunit, origin):
     75          """Converts a .php unit to a .po unit"""
     76          outputunit = po.pounit(encoding="UTF-8")
     77          outputunit.addnote(inputunit.getnotes(origin), origin)
     78          outputunit.addlocation("".join(inputunit.getlocations()))
     79          outputunit.source = inputunit.source
     80          outputunit.target = ""
     81          return outputunit
     82
     83 -def convertphp(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt"):
     84      """reads in inputfile using php, converts using php2po, writes to outputfile"""
     85      inputstore = php.phpfile(inputfile)
     86      convertor = php2po()
     87      if templatefile is None:
     88          outputstore = convertor.convertstore(inputstore, duplicatestyle=duplicatestyle)
     89      else:
     90          templatestore = php.phpfile(templatefile)
     91          outputstore = convertor.mergestore(templatestore, inputstore, blankmsgstr=pot, duplicatestyle=duplicatestyle)
     92      if outputstore.isempty():
     93          return 0
     94      outputfile.write(str(outputstore))
     95      return 1
     96
     97 -def main(argv=None):
     98      from translate.convert import convert
     99      formats = {"php": ("po", convertphp), ("php", "php"): ("po", convertphp)}
    100      parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
    101      parser.add_duplicates_option()
    102      parser.passthrough.append("pot")
    103      parser.run(argv)
    104
    105  if __name__ == '__main__':
    106      main()
    107