Package translate :: Package convert :: Module ini2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.ini2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2007 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 .ini files to Gettext PO localization files"""
     23
     24  import sys
     25  from translate.storage import po
     26  from translate.storage import xliff
     27  from translate.storage import ini
     28
     29 -class ini2po:
     30      """convert a .ini file to a .po file for handling the translation..."""
     31 -    def convertstore(self, theinifile, duplicatestyle="msgctxt"):
     32          """converts a .ini file to a .po file..."""
     33          thetargetfile = po.pofile()
     34          targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit")
     35          targetheader.addnote("extracted from %s" % theinifile.filename, "developer")
     36          thetargetfile.addunit(targetheader)
     37          for iniunit in theinifile.units:
     38              pounit = self.convertunit(iniunit, "developer")
     39              if pounit is not None:
     40                  thetargetfile.addunit(pounit)
     41          thetargetfile.removeduplicates(duplicatestyle)
     42          return thetargetfile
     43
     44 -    def mergestore(self, originifile, translatedinifile, blankmsgstr=False, duplicatestyle="msgctxt"):
     45          """converts two .ini files to a .po file..."""
     46          thetargetfile = po.pofile()
     47          targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit")
     48          targetheader.addnote("extracted from %s, %s" % (originifile.filename, translatedinifile.filename), "developer")
     49          thetargetfile.addunit(targetheader)
     50          translatedinifile.makeindex()
     51          for origini in originifile.units:
     52              origpo = self.convertunit(origini, "developer")
     53              # try and find a translation of the same name...
     54              origininame = "".join(origini.getlocations())
     55              if origininame in translatedinifile.locationindex:
     56                  translatedini = translatedinifile.locationindex[origininame]
     57                  translatedpo = self.convertunit(translatedini, "translator")
     58              else:
     59                  translatedpo = None
     60              # if we have a valid po unit, get the translation and add it...
     61              if origpo is not None:
     62                  if translatedpo is not None and not blankmsgstr:
     63                      origpo.target = translatedpo.source
     64                  thetargetfile.addunit(origpo)
     65              elif translatedpo is not None:
     66                  print >> sys.stderr, "error converting original ini definition %s" % origini.name
     67          thetargetfile.removeduplicates(duplicatestyle)
     68          return thetargetfile
     69
     70 -    def convertunit(self, iniunit, commenttype):
     71          """Converts a .ini unit to a .po unit. Returns None if empty
     72          or not for translation."""
     73          if iniunit is None:
     74              return None
     75          # escape unicode
     76          pounit = po.pounit(encoding="UTF-8")
     77          pounit.addlocation("".join(iniunit.getlocations()))
     78          pounit.source = iniunit.source
     79          pounit.target = ""
     80          return pounit
     81
     82 -def convertini(inputfile, outputfile, templatefile, pot=False, duplicatestyle="msgctxt"):
     83      """reads in inputfile using ini, converts using ini2po, writes to outputfile"""
     84      inputstore = ini.inifile(inputfile)
     85      convertor = ini2po()
     86      if templatefile is None:
     87          outputstore = convertor.convertstore(inputstore, duplicatestyle=duplicatestyle)
     88      else:
     89          templatestore = ini.inifile(templatefile)
     90          outputstore = convertor.mergestore(templatestore, inputstore, blankmsgstr=pot, duplicatestyle=duplicatestyle)
     91      if outputstore.isempty():
     92          return 0
     93      outputfile.write(str(outputstore))
     94      return 1
     95
     96 -def main(argv=None):
     97      from translate.convert import convert
     98      formats = {"ini": ("po", convertini), ("ini", "ini"): ("po", convertini)}
     99      parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
    100      parser.add_duplicates_option()
    101      parser.passthrough.append("pot")
    102      parser.run(argv)
    103
    104  if __name__ == '__main__':
    105      main()
    106