translate.convert.po2rc

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 -class rerc:
     31 -    def __init__(self, templatefile, charset="utf-8", lang=None, sublang=None):
     32          self.templatefile = templatefile
     33          self.templatestore = rc.rcfile(templatefile, encoding=charset)
     34          self.inputdict = {}
     35          self.charset = charset
     36          self.lang = lang
     37          self.sublang = sublang
     38
     39 -    def convertstore(self, inputstore, includefuzzy=False):
     40          self.makestoredict(inputstore, includefuzzy)
     41          outputblocks = []
     42          for block in self.templatestore.blocks:
     43              outputblocks.append(self.convertblock(block))
     44          if self.charset == "utf-8":
     45              outputblocks.insert(0, "#pragma code_page(65001)\n")
     46              outputblocks.append("#pragma code_page(default)")
     47          return outputblocks
     48
     49 -    def makestoredict(self, store, includefuzzy=False):
     50          """ make a dictionary of the translations"""
     51          for unit in store.units:
     52              if includefuzzy or not unit.isfuzzy():
     53                  for location in unit.getlocations():
     54                      rcstring = unit.target
     55                      if len(rcstring.strip()) == 0:
     56                          rcstring = unit.source
     57                      self.inputdict[location] = rc.escape_to_rc(rcstring).encode(self.charset)
     58
     59 -    def convertblock(self, block):
     60          newblock = block
     61          if isinstance(newblock, unicode):
     62              newblock = newblock.encode('utf-8')
     63          if newblock.startswith("LANGUAGE"):
     64              return "LANGUAGE %s, %s" % (self.lang, self.sublang)
     65          for unit in self.templatestore.units:
     66              location = unit.getlocations()[0]
     67              if self.inputdict.has_key(location):
     68                  if self.inputdict[location] != unit.match.groupdict()['value']:
     69                      newmatch = unit.match.group().replace(unit.match.groupdict()['value'], self.inputdict[location])
     70                      newblock = newblock.replace(unit.match.group(), newmatch)
     71          if isinstance(newblock, unicode):
     72              newblock = newblock.encode(self.charset)
     73          return newblock
     74
     75 -def convertrc(inputfile, outputfile, templatefile, includefuzzy=False, charset=None, lang=None, sublang=None):
     76      inputstore = po.pofile(inputfile)
     77      if not lang:
     78          raise ValueError("must specify a target language")
     79      if templatefile is None:
     80          raise ValueError("must have template file for rc files")
     81          # convertor = po2rc()
     82      else:
     83          convertor = rerc(templatefile, charset, lang, sublang)
     84      outputrclines = convertor.convertstore(inputstore, includefuzzy)
     85      outputfile.writelines(outputrclines)
     86      return 1
     87
     88 -def main(argv=None):
     89      # handle command line options
     90      from translate.convert import convert
     91      formats = {("po", "rc"): ("rc", convertrc)}
     92      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
     93      defaultcharset = "utf-8"
     94      parser.add_option("", "--charset", dest="charset", default=defaultcharset,
     95          help="charset to use to decode the RC files (default: %s)" % defaultcharset, metavar="CHARSET")
     96      parser.add_option("-l", "--lang", dest="lang", default=None,
     97          help="LANG entry", metavar="LANG")
     98      defaultsublang = "SUBLANG_DEFAULT"
     99      parser.add_option("", "--sublang", dest="sublang", default=defaultsublang,
    100          help="SUBLANG entry (default: %s)" % defaultsublang, metavar="SUBLANG")
    101      parser.passthrough.append("charset")
    102      parser.passthrough.append("lang")
    103      parser.passthrough.append("sublang")
    104      parser.add_fuzzy_option()
    105      parser.run(argv)
    106
    107  if __name__ == '__main__':
    108      main()
    109