translate.tools.pomerge

Package translate :: Package tools :: Module pomerge |
[hide private]
[frames] | no frames]

# Source Code for Module translate.tools.pomerge

      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  """Merges XLIFF and Gettext PO localization files
     23
     24  Snippet file produced by pogrep or updated by a translator can be merged into
     25  existing files
     26
     27  See: http://translate.sourceforge.net/wiki/toolkit/pomerge for examples and
     28  usage instructions
     29  """
     30
     31  import sys
     32  from translate.storage import factory
     33  from translate.storage.poheader import poheader
     34
     35 -def mergestores(store1, store2, mergeblanks, mergecomments):
     36      """Take any new translations in store2 and write them into store1."""
     37
     38      for unit2 in store2.units:
     39          if unit2.isheader():
     40              if isinstance(store1, poheader):
     41                  store1.mergeheaders(store2)
     42              # Skip header units
     43              continue
     44          # there may be more than one entity due to msguniq merge
     45          entities = unit2.getlocations()
     46          if len(entities) == 0:
     47              source = unit2.source
     48              unit1 = store1.findunit(source)
     49              if unit1 is None:
     50                  sys.stderr.write(str(unit2) + "\n")
     51              else:
     52                  # finally set the new definition in unit1
     53                  unit1.merge(unit2, overwrite=True)
     54          for entity in entities:
     55              unit1 = None
     56              if store1.locationindex.has_key(entity):
     57                  # now we need to replace the definition of entity with msgstr
     58                  unit1 = store1.locationindex[entity] # find the other po
     59              # check if this is a duplicate in store2...
     60              if store2.locationindex.has_key(entity):
     61                  if store2.locationindex[entity] is None:
     62                      unit1 = None
     63              # if locationindex was not unique, use the source index
     64              if unit1 is None:
     65                  source = unit2.source
     66                  unit1 = store1.findunit(source)
     67              # check if we found a matching po element
     68              if unit1 is None:
     69                  print >> sys.stderr, "# the following po element was not found"
     70                  sys.stderr.write(str(unit2) + "\n")
     71              else:
     72                  if not mergeblanks:
     73                      target = unit2.target
     74                      if len(target.strip()) == 0:
     75                          continue
     76                  # finally set the new definition in unit1
     77                  unit1.merge(unit2, overwrite=True, comments=mergecomments)
     78      return store1
     79
     80 -def str2bool(option):
     81      """Convert a string value to boolean
     82
     83      @param option: yes, true, 1, no, false, 0
     84      @type option: String
     85      @rtype: Boolean
     86
     87      """
     88      option = option.lower()
     89      if option in ("yes", "true", "1"):
     90          return True
     91      elif option in ("no", "false", "0"):
     92          return False
     93      else:
     94          raise ValueError("invalid boolean value: %r" % option)
     95
     96 -def mergestore(inputfile, outputfile, templatefile, mergeblanks="no", mergecomments="yes"):
     97      try:
     98          mergecomments = str2bool(mergecomments)
     99      except ValueError:
    100          raise ValueError("invalid mergecomments value: %r" % mergecomments)
    101      try:
    102          mergeblanks = str2bool(mergeblanks)
    103      except ValueError:
    104          raise ValueError("invalid mergeblanks value: %r" % mergeblanks)
    105      inputstore = factory.getobject(inputfile)
    106      if templatefile is None:
    107          # just merge nothing
    108          templatestore = type(inputstore)()
    109      else:
    110          templatestore = factory.getobject(templatefile)
    111      templatestore.makeindex()
    112      inputstore.makeindex()
    113      outputstore = mergestores(templatestore, inputstore, mergeblanks, mergecomments)
    114      if outputstore.isempty():
    115          return 0
    116      outputfile.write(str(outputstore))
    117      return 1
    118
    119 -def main():
    120      from translate.convert import convert
    121      pooutput = ("po", mergestore)
    122      potoutput = ("pot", mergestore)
    123      xliffoutput = ("xlf", mergestore)
    124      formats = {("po", "po"): pooutput, ("po", "pot"): pooutput, ("pot", "po"): pooutput, ("pot", "pot"): potoutput,
    125                  "po": pooutput, "pot": pooutput,
    126                  ("xlf", "po"): pooutput, ("xlf", "pot"): pooutput,
    127                  ("xlf", "xlf"): xliffoutput, ("po", "xlf"): xliffoutput}
    128      mergeblanksoption = convert.optparse.Option("", "--mergeblanks", dest="mergeblanks",
    129          action="store", default="yes", help="whether to overwrite existing translations with blank translations (yes/no). Default is yes.")
    130      mergecommentsoption = convert.optparse.Option("", "--mergecomments", dest="mergecomments",
    131          action="store", default="yes", help="whether to merge comments as well as translations (yes/no). Default is yes.")
    132      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
    133      parser.add_option(mergeblanksoption)
    134      parser.passthrough.append("mergeblanks")
    135      parser.add_option(mergecommentsoption)
    136      parser.passthrough.append("mergecomments")
    137      parser.run()
    138
    139
    140  if __name__ == '__main__':
    141      main()
    142