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-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 .rc files to Gettext PO localization files"""
     22
     23  import sys
     24
     25  from translate.storage import po
     26  from translate.storage import rc
     27
     28
     29 -class rc2po:
     30      """Convert a .rc file to a .po file for handling the translation."""
     31
     32 -    def convert_store(self, input_store, duplicatestyle="msgctxt"):
     33          """converts a .rc file to a .po file..."""
     34          output_store = po.pofile()
     35          output_header = output_store.init_headers(charset="UTF-8", encoding="8bit")
     36          output_header.addnote("extracted from %s" % input_store.filename, "developer")
     37          for input_unit in input_store.units:
     38              output_unit = self.convert_unit(input_unit, "developer")
     39              if output_unit is not None:
     40                  output_store.addunit(output_unit)
     41          output_store.removeduplicates(duplicatestyle)
     42          return output_store
     43
     44 -    def merge_store(self, template_store, input_store, blankmsgstr=False, duplicatestyle="msgctxt"):
     45          """converts two .rc files to a .po file..."""
     46          output_store = po.pofile()
     47          output_header = output_store.init_headers(charset="UTF-8", encoding="8bit")
     48          output_header.addnote("extracted from %s, %s" % (template_store.filename, input_store.filename), "developer")
     49          input_store.makeindex()
     50          for template_unit in template_store.units:
     51              origpo = self.convert_unit(template_unit, "developer")
     52              # try and find a translation of the same name...
     53              template_unit_name = "".join(template_unit.getlocations())
     54              if template_unit_name in input_store.locationindex:
     55                  translatedrc = input_store.locationindex[template_unit_name]
     56                  translatedpo = self.convert_unit(translatedrc, "translator")
     57              else:
     58                  translatedpo = None
     59              # if we have a valid po unit, get the translation and add it...
     60              if origpo is not None:
     61                  if translatedpo is not None and not blankmsgstr:
     62                      origpo.target = translatedpo.source
     63                  output_store.addunit(origpo)
     64              elif translatedpo is not None:
     65                  print >> sys.stderr, "error converting original rc definition %s" % template_unit.name
     66          output_store.removeduplicates(duplicatestyle)
     67          return output_store
     68
     69 -    def convert_unit(self, input_unit, commenttype):
     70          """Converts a .rc unit to a .po unit. Returns None if empty
     71          or not for translation."""
     72          if input_unit is None:
     73              return None
     74          # escape unicode
     75          output_unit = po.pounit(encoding="UTF-8")
     76          output_unit.addlocation("".join(input_unit.getlocations()))
     77          output_unit.source = input_unit.source
     78          output_unit.target = ""
     79          return output_unit
     80
     81
     82 -def convertrc(input_file, output_file, template_file, pot=False, duplicatestyle="msgctxt", charset=None, lang=None, sublang=None):
     83      """reads in input_file using rc, converts using rc2po, writes to output_file"""
     84      input_store = rc.rcfile(input_file, lang, sublang, encoding=charset)
     85      convertor = rc2po()
     86      if template_file is None:
     87          output_store = convertor.convert_store(input_store, duplicatestyle=duplicatestyle)
     88      else:
     89          template_store = rc.rcfile(template_file, lang, sublang, encoding=charset)
     90          output_store = convertor.merge_store(template_store, input_store, blankmsgstr=pot, duplicatestyle=duplicatestyle)
     91      if output_store.isempty():
     92          return 0
     93      output_file.write(str(output_store))
     94      return 1
     95
     96
     97 -def main(argv=None):
     98      from translate.convert import convert
     99      formats = {"rc": ("po", convertrc), ("rc", "rc"): ("po", convertrc),
    100                 "nls": ("po", convertrc), ("nls", "nls"): ("po", convertrc)}
    101      parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
    102      DEFAULTCHARSET = "cp1252"
    103      parser.add_option("", "--charset", dest="charset", default=DEFAULTCHARSET,
    104          help="charset to use to decode the RC files (default: %s)" % DEFAULTCHARSET, metavar="CHARSET")
    105      DEFAULTLANG = "LANG_ENGLISH"
    106      parser.add_option("-l", "--lang", dest="lang", default=DEFAULTLANG,
    107          help="LANG entry (default: %s)" % DEFAULTLANG, metavar="LANG")
    108      DEFAULTSUBLANG = "SUBLANG_DEFAULT"
    109      parser.add_option("", "--sublang", dest="sublang", default=DEFAULTSUBLANG,
    110          help="SUBLANG entry (default: %s)" % DEFAULTSUBLANG, metavar="SUBLANG")
    111      parser.add_duplicates_option()
    112      parser.passthrough.append("pot")
    113      parser.passthrough.append("charset")
    114      parser.passthrough.append("lang")
    115      parser.passthrough.append("sublang")
    116      parser.run(argv)
    117
    118
    119  if __name__ == '__main__':
    120      main()
    121