Package translate :: Package convert :: Module rc2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.rc2po

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