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 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
     23  """convert Gettext PO localization files to Windows Resource (.rc) files
     24
     25  see: http://translate.sourceforge.net/wiki/toolkit/po2rc for examples and
     26  usage instructions
     27  """
     28
     29  from translate.misc import quote
     30  from translate.storage import po
     31  from translate.storage import rc
     32
     33  eol = "\n"
     34
     35 -class rerc:
     36 -    def __init__(self, templatefile, charset="utf-8", lang=None, sublang=None):
     37          self.templatefile = templatefile
     38          self.templatestore = rc.rcfile(templatefile)
     39          self.inputdict = {}
     40          self.charset = charset
     41          self.lang = lang
     42          self.sublang = sublang
     43
     44 -    def convertstore(self, inputstore, includefuzzy=False):
     45          self.makestoredict(inputstore, includefuzzy)
     46          outputblocks = []
     47          for block in self.templatestore.blocks:
     48              outputblocks.append(self.convertblock(block))
     49          if self.charset == "utf-8":
     50              outputblocks.insert(0, "#pragma code_page(65001)\n")
     51              outputblocks.append("#pragma code_page(default)")
     52          return outputblocks
     53
     54 -    def makestoredict(self, store, includefuzzy=False):
     55          """ make a dictionary of the translations"""
     56          for unit in store.units:
     57              if includefuzzy or not unit.isfuzzy():
     58                  for location in unit.getlocations():
     59                      rcstring = unit.target
     60                      if len(rcstring.strip()) == 0:
     61                          rcstring = unit.source
     62                      self.inputdict[location] = rc.escape_to_rc(rcstring)
     63
     64 -    def convertblock(self, block):
     65          newblock = block
     66          if isinstance(newblock, unicode):
     67              newblock = newblock.encode('utf-8')
     68          if newblock.startswith("LANGUAGE"):
     69              return "LANGUAGE %s, %s" % (self.lang, self.sublang)
     70          for unit in self.templatestore.units:
     71              location = unit.getlocations()[0]
     72              if self.inputdict.has_key(location):
     73                  if self.inputdict[location] != unit.match.groupdict()['value']:
     74                      newmatch = unit.match.group().replace(unit.match.groupdict()['value'], self.inputdict[location])
     75                      newblock = newblock.replace(unit.match.group(), newmatch)
     76          if isinstance(newblock, unicode):
     77              newblock = newblock.encode(self.charset)
     78          return newblock
     79
     80 -def convertrc(inputfile, outputfile, templatefile, includefuzzy=False, 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 -def main(argv=None):
     94      # handle command line options
     95      from translate.convert import convert
     96      formats = {("po", "rc"): ("rc", convertrc)}
     97      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
     98      defaultcharset = "utf-8"
     99      parser.add_option("", "--charset", dest="charset", default=defaultcharset,
    100          help="charset to use to decode the RC files (default: %s)" % defaultcharset, metavar="CHARSET")
    101      parser.add_option("-l", "--lang", dest="lang", default=None,
    102          help="LANG entry", metavar="LANG")
    103      defaultsublang="SUBLANG_DEFAULT"
    104      parser.add_option("", "--sublang", dest="sublang", default=defaultsublang,
    105          help="SUBLANG entry (default: %s)" % defaultsublang, metavar="SUBLANG")
    106      parser.passthrough.append("charset")
    107      parser.passthrough.append("lang")
    108      parser.passthrough.append("sublang")
    109      parser.add_fuzzy_option()
    110      parser.run(argv)
    111
    112  if __name__ == '__main__':
    113      main()
    114