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

# Source Code for Module translate.convert.po2rc

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2002-2006,2008-2009 Zuza Software Foundation
      5  #
      6  # This file is part of the Translate Toolkit.
      7  #
      8  # This program 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  # This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
     20
     21  """Convert Gettext PO localization files back to Windows Resource (.rc) files
     22
     23  See: http://translate.sourceforge.net/wiki/toolkit/po2rc for examples and
     24  usage instructions.
     25  """
     26
     27  from translate.storage import po
     28  from translate.storage import rc
     29
     30
     31 -class rerc:
     32
     33 -    def __init__(self, templatefile, charset="utf-8", lang=None, sublang=None):
     34          self.templatefile = templatefile
     35          self.templatestore = rc.rcfile(templatefile, encoding=charset)
     36          self.inputdict = {}
     37          self.charset = charset
     38          self.lang = lang
     39          self.sublang = sublang
     40
     41 -    def convertstore(self, inputstore, includefuzzy=False):
     42          self.makestoredict(inputstore, includefuzzy)
     43          outputblocks = []
     44          for block in self.templatestore.blocks:
     45              outputblocks.append(self.convertblock(block))
     46          if self.charset == "utf-8":
     47              outputblocks.insert(0, "#pragma code_page(65001)\n")
     48              outputblocks.append("#pragma code_page(default)")
     49          return outputblocks
     50
     51 -    def makestoredict(self, store, includefuzzy=False):
     52          """ make a dictionary of the translations"""
     53          for unit in store.units:
     54              if includefuzzy or not unit.isfuzzy():
     55                  for location in unit.getlocations():
     56                      rcstring = unit.target
     57                      if len(rcstring.strip()) == 0:
     58                          rcstring = unit.source
     59                      self.inputdict[location] = rc.escape_to_rc(rcstring).encode(self.charset)
     60
     61 -    def convertblock(self, block):
     62          newblock = block
     63          if isinstance(newblock, unicode):
     64              newblock = newblock.encode('utf-8')
     65          if newblock.startswith("LANGUAGE"):
     66              return "LANGUAGE %s, %s" % (self.lang, self.sublang)
     67          for unit in self.templatestore.units:
     68              location = unit.getlocations()[0]
     69              if location in self.inputdict:
     70                  if self.inputdict[location] != unit.match.groupdict()['value']:
     71                      newmatch = unit.match.group().replace(unit.match.groupdict()['value'],
     72                                                            self.inputdict[location])
     73                      newblock = newblock.replace(unit.match.group(), newmatch)
     74          if isinstance(newblock, unicode):
     75              newblock = newblock.encode(self.charset)
     76          return newblock
     77
     78
     79 -def convertrc(inputfile, outputfile, templatefile, includefuzzy=False,
     80                charset=None, lang=None, sublang=None):
     81      inputstore = po.pofile(inputfile)
     82      if not lang:
     83          raise ValueError("must specify a target language")
     84      if templatefile is None:
     85          raise ValueError("must have template file for rc files")
     86          # convertor = po2rc()
     87      else:
     88          convertor = rerc(templatefile, charset, lang, sublang)
     89      outputrclines = convertor.convertstore(inputstore, includefuzzy)
     90      outputfile.writelines(outputrclines)
     91      return 1
     92
     93
     94 -def main(argv=None):
     95      # handle command line options
     96      from translate.convert import convert
     97      formats = {("po", "rc"): ("rc", convertrc)}
     98      parser = convert.ConvertOptionParser(formats, usetemplates=True,
     99                                           description=__doc__)
    100      defaultcharset = "utf-8"
    101      parser.add_option("", "--charset", dest="charset", default=defaultcharset,
    102          help="charset to use to decode the RC files (default: %s)" % defaultcharset,
    103          metavar="CHARSET")
    104      parser.add_option("-l", "--lang", dest="lang", default=None,
    105          help="LANG entry", metavar="LANG")
    106      defaultsublang = "SUBLANG_DEFAULT"
    107      parser.add_option("", "--sublang", dest="sublang", default=defaultsublang,
    108          help="SUBLANG entry (default: %s)" % defaultsublang, metavar="SUBLANG")
    109      parser.passthrough.append("charset")
    110      parser.passthrough.append("lang")
    111      parser.passthrough.append("sublang")
    112      parser.add_fuzzy_option()
    113      parser.run(argv)
    114
    115  if __name__ == '__main__':
    116      main()
    117