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 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 # Skip header units 45 continue 46 # there may be more than one entity due to msguniq merge 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 # finally set the new definition in unit1 57 unit1.merge(unit2, overwrite=True) 58 for entity in entities: 59 unit1 = None 60 if store1.locationindex.has_key(entity): 61 # now we need to replace the definition of entity with msgstr 62 unit1 = store1.locationindex[entity] # find the other po 63 # check if this is a duplicate in store2... 64 if store2.locationindex.has_key(entity): 65 if store2.locationindex[entity] is None: 66 unit1 = None 67 # if locationindex was not unique, use the sourceindex 68 if unit1 is None: 69 source = unit2.source 70 if source in store1.sourceindex: 71 unit1 = store1.sourceindex[source] 72 # check if we found a matching po element 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 # finally set the new definition in unit1 81 unit1.merge(unit2, overwrite=True, comments=mergecomments) 82 return store1
83
84 -def str2bool(option):
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 # just merge nothing 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
123 -def main():
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