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