1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 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 import po 
 34  from translate.storage import xliff  
 35  from translate.storage.poheader import poheader 
 36   
 37 -def mergestores(store1, store2, mergeblanks, mergecomments): 
  38      """Take any new translations in store2 and write them into store1.""" 
 39   
 40      for unit2 in store2.units: 
 41          if unit2.isheader(): 
 42              if isinstance(store1, poheader): 
 43                  store1.mergeheaders(store2) 
 44               
 45              continue 
 46           
 47          entities = unit2.getlocations() 
 48          if len(entities) == 0: 
 49              source = unit2.source 
 50              unit1 = None 
 51              if source in store1.sourceindex: 
 52                  unit1 = store1.sourceindex[source] 
 53              if unit1 is None: 
 54                  sys.stderr.write(str(unit2) + "\n") 
 55              else: 
 56                   
 57                  unit1.merge(unit2, overwrite=True) 
 58          for entity in entities: 
 59              unit1 = None 
 60              if store1.locationindex.has_key(entity): 
 61                   
 62                  unit1 = store1.locationindex[entity]  
 63               
 64              if store2.locationindex.has_key(entity): 
 65                  if store2.locationindex[entity] is None: 
 66                      unit1 = None 
 67               
 68              if unit1 is None: 
 69                  source = unit2.source 
 70                  if source in store1.sourceindex: 
 71                      unit1 = store1.sourceindex[source] 
 72               
 73              if unit1 is None: 
 74                  print >> sys.stderr, "# the following po element was not found" 
 75                  sys.stderr.write(str(unit2) + "\n") 
 76              else: 
 77                  if not mergeblanks: 
 78                      target = unit2.target 
 79                      if len(target.strip()) == 0: continue 
 80                   
 81                  unit1.merge(unit2, overwrite=True, comments=mergecomments) 
 82      return store1 
  83   
 85      """Convert a string value to boolean 
 86   
 87      @param option: yes, true, 1, no, false, 0 
 88      @type option: String 
 89      @rtype: Boolean 
 90   
 91      """ 
 92      option = option.lower() 
 93      if option in ("yes", "true", "1"): 
 94          return True 
 95      elif option in ("no", "false", "0"): 
 96          return False 
 97      else: 
 98          raise ValueError("invalid boolean value: %r" % option) 
  99   
100 -def mergestore(inputfile, outputfile, templatefile, mergeblanks="no", mergecomments="yes"): 
 101      try: 
102          mergecomments = str2bool(mergecomments) 
103      except ValueError: 
104          raise ValueError("invalid mergecomments value: %r" % mergecomments) 
105      try: 
106          mergeblanks = str2bool(mergeblanks) 
107      except ValueError: 
108          raise ValueError("invalid mergeblanks value: %r" % mergeblanks) 
109      inputstore = factory.getobject(inputfile) 
110      if templatefile is None: 
111           
112          templatestore = type(inputstore)() 
113      else: 
114          templatestore = factory.getobject(templatefile) 
115      templatestore.makeindex() 
116      inputstore.makeindex() 
117      outputstore = mergestores(templatestore, inputstore, mergeblanks, mergecomments) 
118      if outputstore.isempty(): 
119          return 0 
120      outputfile.write(str(outputstore)) 
121      return 1 
 122   
124      from translate.convert import convert 
125      pooutput = ("po", mergestore) 
126      potoutput = ("pot", mergestore) 
127      xliffoutput = ("xlf", mergestore) 
128      formats = {("po", "po"): pooutput, ("po", "pot"): pooutput, ("pot", "po"): pooutput, ("pot", "pot"): potoutput, 
129                  "po": pooutput, "pot": pooutput, 
130                  ("xlf", "po"): pooutput, ("xlf", "pot"): pooutput, 
131                  ("xlf", "xlf"): xliffoutput, ("po", "xlf"): xliffoutput} 
132      mergeblanksoption = convert.optparse.Option("", "--mergeblanks", dest="mergeblanks", 
133          action="store", default="yes", help="whether to overwrite existing translations with blank translations (yes/no)") 
134      mergecommentsoption = convert.optparse.Option("", "--mergecomments", dest="mergecomments", 
135          action="store", default="yes", help="whether to merge comments as well as translations (yes/no)") 
136      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__) 
137      parser.add_option(mergeblanksoption) 
138      parser.passthrough.append("mergeblanks") 
139      parser.add_option(mergecommentsoption) 
140      parser.passthrough.append("mergecomments") 
141      parser.run() 
 142   
143   
144  if __name__ == '__main__': 
145      main() 
146