Package translate :: Package convert :: Module oo2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.oo2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2003-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
     23  """convert an OpenOffice.org (SDF) localization file to Gettext PO localization files
     24
     25  See: http://translate.sourceforge.net/wiki/toolkit/oo2po for examples and
     26  usage instructions
     27  """
     28
     29  import sys
     30  from urllib import urlencode
     31
     32  from translate.storage import po
     33  from translate.storage import oo
     34
     35  # TODO: support using one GSI file as template, another as input (for when English is in one and translation in another)
     36
     37
     38 -class oo2po:
     39
     40 -    def __init__(self, sourcelanguage, targetlanguage, blankmsgstr=False, long_keys=False):
     41          """construct an oo2po converter for the specified languages"""
     42          self.sourcelanguage = sourcelanguage
     43          self.targetlanguage = targetlanguage
     44          self.blankmsgstr = blankmsgstr
     45          self.long_keys = long_keys
     46
     47 -    def maketargetunit(self, part1, part2, translators_comment, key, subkey):
     48          """makes a base unit (.po or XLIFF) out of a subkey of two parts"""
     49          #TODO: Do better
     50          text1 = getattr(part1, subkey)
     51          if text1 == "":
     52              return None
     53          text2 = getattr(part2, subkey)
     54
     55          unit = po.pounit(text1.decode('utf-8'), encoding="UTF-8")
     56          unit.target = text2.decode('utf-8')
     57          unit.addlocation(key + "." + subkey)
     58          if getattr(translators_comment, subkey).strip() != "":
     59              unit.addnote(getattr(translators_comment, subkey), origin="developer")
     60          return unit
     61
     62 -    def convertelement(self, theoo):
     63          """convert an oo element into a list of base units (.po or XLIFF)"""
     64          if self.sourcelanguage in theoo.languages:
     65              part1 = theoo.languages[self.sourcelanguage]
     66          else:
     67              print >> sys.stderr, "/".join(theoo.lines[0].getkey()), "language not found: %s" % (self.sourcelanguage)
     68              return []
     69          if self.blankmsgstr:
     70              # use a blank part2
     71              part2 = oo.ooline()
     72          else:
     73              if self.targetlanguage in theoo.languages:
     74                  part2 = theoo.languages[self.targetlanguage]
     75              else:
     76                  # if the language doesn't exist, the translation is missing ... so make it blank
     77                  part2 = oo.ooline()
     78          if "x-comment" in theoo.languages:
     79              translators_comment = theoo.languages["x-comment"]
     80          else:
     81              translators_comment = oo.ooline()
     82          key = oo.makekey(part1.getkey(), self.long_keys)
     83          unitlist = []
     84          for subkey in ("text", "quickhelptext", "title"):
     85              unit = self.maketargetunit(part1, part2, translators_comment,
     86                                         key, subkey)
     87              if unit is not None:
     88                  unitlist.append(unit)
     89          return unitlist
     90
     91 -    def convertstore(self, theoofile, duplicatestyle="msgctxt"):
     92          """converts an entire oo file to a base class format (.po or XLIFF)"""
     93          thetargetfile = po.pofile()
     94          # create a header for the file
     95          bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi?%s' % \
     96                    urlencode({"subcomponent": "ui",
     97                               "comment": "",
     98                               "short_desc": "Localization issue in file: %s" % \
     99                                             theoofile.filename,
    100                               "component": "l10n",
    101                               "form_name": "enter_issue",
    102                              })
    103          targetheader = thetargetfile.init_headers(charset="UTF-8",
    104                                                    encoding="8bit",
    105                                                    x_accelerator_marker="~",
    106                                                    report_msgid_bugs_to=bug_url)
    107          targetheader.addnote("extracted from %s" % theoofile.filename,
    108                               "developer")
    109          thetargetfile.setsourcelanguage(self.sourcelanguage)
    110          thetargetfile.settargetlanguage(self.targetlanguage)
    111          # go through the oo and convert each element
    112          for theoo in theoofile.units:
    113              unitlist = self.convertelement(theoo)
    114              for unit in unitlist:
    115                  thetargetfile.addunit(unit)
    116          thetargetfile.removeduplicates(duplicatestyle)
    117          return thetargetfile
    118
    119
    120 -def verifyoptions(options):
    121      """verifies the commandline options"""
    122      if not options.pot and not options.targetlanguage:
    123          raise ValueError("You must specify the target language unless generating POT files (-P)")
    124
    125
    126 -def convertoo(inputfile, outputfile, templates, pot=False, sourcelanguage=None, targetlanguage=None, duplicatestyle="msgid_comment", multifilestyle="single"):
    127      """reads in stdin using inputstore class, converts using convertorclass, writes to stdout"""
    128      inputstore = oo.oofile()
    129      if hasattr(inputfile, "filename"):
    130          inputfilename = inputfile.filename
    131      else:
    132          inputfilename = "(input file name not known)"
    133      inputstore.filename = inputfilename
    134      inputstore.parse(inputfile.read())
    135      if not sourcelanguage:
    136          testlangtype = targetlanguage or (inputstore and inputstore.languages[0]) or ""
    137          if testlangtype.isdigit():
    138              sourcelanguage = "01"
    139          else:
    140              sourcelanguage = "en-US"
    141      if not sourcelanguage in inputstore.languages:
    142          print >> sys.stderr, "Warning: sourcelanguage '%s' not found in inputfile '%s' (contains %s)" % (sourcelanguage, inputfilename, ", ".join(inputstore.languages))
    143      if targetlanguage and targetlanguage not in inputstore.languages:
    144          print >> sys.stderr, "Warning: targetlanguage '%s' not found in inputfile '%s' (contains %s)" % (targetlanguage, inputfilename, ", ".join(inputstore.languages))
    145      convertor = oo2po(sourcelanguage, targetlanguage, blankmsgstr=pot, long_keys=multifilestyle != "single")
    146      outputstore = convertor.convertstore(inputstore, duplicatestyle)
    147      if outputstore.isempty():
    148          return 0
    149      outputfile.write(str(outputstore))
    150      return 1
    151
    152
    153 -def main(argv=None):
    154      from translate.convert import convert
    155      formats = {"oo": ("po", convertoo), "sdf": ("po", convertoo)}
    156      # always treat the input as an archive unless it is a directory
    157      archiveformats = {(None, "input"): oo.oomultifile}
    158      parser = convert.ArchiveConvertOptionParser(formats, usepots=True, description=__doc__, archiveformats=archiveformats)
    159      parser.add_option("-l", "--language", dest="targetlanguage", default=None,
    160              help="set target language to extract from oo file (e.g. af-ZA)", metavar="LANG")
    161      parser.add_option("", "--source-language", dest="sourcelanguage", default=None,
    162              help="set source language code (default en-US)", metavar="LANG")
    163      parser.add_option("", "--nonrecursiveinput", dest="allowrecursiveinput", default=True, action="store_false", help="don't treat the input oo as a recursive store")
    164      parser.add_duplicates_option()
    165      parser.add_multifile_option()
    166      parser.passthrough.append("pot")
    167      parser.passthrough.append("sourcelanguage")
    168      parser.passthrough.append("targetlanguage")
    169      parser.verifyoptions = verifyoptions
    170      parser.run(argv)
    171
    172
    173  if __name__ == '__main__':
    174      main()
    175