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-2010 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 logging 
 32   
 33  from translate.storage import factory 
 34  from translate.storage.poheader import poheader 
 35   
 36   
37 -def mergestores(store1, store2, mergeblanks, mergefuzzy, mergecomments):
38 """Take any new translations in store2 and write them into store1.""" 39 print mergefuzzy 40 41 for unit2 in store2.units: 42 if unit2.isheader(): 43 if isinstance(store1, poheader): 44 store1.mergeheaders(store2) 45 continue 46 unit1 = store1.findid(unit2.getid()) 47 if unit1 is None: 48 unit1 = store1.findunit(unit2.source) 49 if unit1 is None: 50 logging.error("The template does not contain the following unit:\n%s", 51 str(unit2)) 52 else: 53 if not mergeblanks: 54 if len(unit2.target.strip()) == 0: 55 continue 56 if not mergefuzzy: 57 if unit2.isfuzzy(): 58 continue 59 unit1.merge(unit2, overwrite=True, comments=mergecomments) 60 return store1
61 62
63 -def str2bool(option):
64 """Convert a string value to boolean 65 66 @param option: yes, true, 1, no, false, 0 67 @type option: String 68 @rtype: Boolean 69 70 """ 71 option = option.lower() 72 if option in ("yes", "true", "1"): 73 return True 74 elif option in ("no", "false", "0"): 75 return False 76 else: 77 raise ValueError("invalid boolean value: %r" % option)
78 79
80 -def mergestore(inputfile, outputfile, templatefile, mergeblanks="no", mergefuzzy="no", 81 mergecomments="yes"):
82 try: 83 mergecomments = str2bool(mergecomments) 84 except ValueError: 85 raise ValueError("invalid mergecomments value: %r" % mergecomments) 86 try: 87 mergeblanks = str2bool(mergeblanks) 88 except ValueError: 89 raise ValueError("invalid mergeblanks value: %r" % mergeblanks) 90 try: 91 mergefuzzy = str2bool(mergefuzzy) 92 except ValueError: 93 raise ValueError("invalid mergefuzzy value: %r" % mergefuzzy) 94 inputstore = factory.getobject(inputfile) 95 if templatefile is None: 96 # just merge nothing 97 templatestore = type(inputstore)() 98 else: 99 templatestore = factory.getobject(templatefile) 100 outputstore = mergestores(templatestore, inputstore, mergeblanks, 101 mergefuzzy, mergecomments) 102 if outputstore.isempty(): 103 return 0 104 outputfile.write(str(outputstore)) 105 return 1
106 107
108 -def main():
109 from translate.convert import convert 110 pooutput = ("po", mergestore) 111 potoutput = ("pot", mergestore) 112 xliffoutput = ("xlf", mergestore) 113 formats = {("po", "po"): pooutput, ("po", "pot"): pooutput, 114 ("pot", "po"): pooutput, ("pot", "pot"): potoutput, 115 "po": pooutput, "pot": pooutput, 116 ("xlf", "po"): pooutput, ("xlf", "pot"): pooutput, 117 ("xlf", "xlf"): xliffoutput, ("po", "xlf"): xliffoutput, 118 } 119 mergeblanksoption = convert.optparse.Option("", "--mergeblanks", 120 dest="mergeblanks", action="store", default="yes", 121 help="whether to overwrite existing translations with blank translations (yes/no). Default is yes.") 122 mergefuzzyoption = convert.optparse.Option("", "--mergefuzzy", 123 dest="mergefuzzy", action="store", default="yes", 124 help="whether to consider fuzzy translations from input (yes/no). Default is yes.") 125 mergecommentsoption = convert.optparse.Option("", "--mergecomments", 126 dest="mergecomments", action="store", default="yes", 127 help="whether to merge comments as well as translations (yes/no). Default is yes.") 128 parser = convert.ConvertOptionParser(formats, usetemplates=True, 129 description=__doc__) 130 parser.add_option(mergeblanksoption) 131 parser.passthrough.append("mergeblanks") 132 parser.add_option(mergefuzzyoption) 133 parser.passthrough.append("mergefuzzy") 134 parser.add_option(mergecommentsoption) 135 parser.passthrough.append("mergecomments") 136 parser.run()
137 138 139 if __name__ == '__main__': 140 main() 141