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.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
43 continue
44
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
53 unit1.merge(unit2, overwrite=True)
54 for entity in entities:
55 unit1 = None
56 if store1.locationindex.has_key(entity):
57
58 unit1 = store1.locationindex[entity]
59
60 if store2.locationindex.has_key(entity):
61 if store2.locationindex[entity] is None:
62 unit1 = None
63
64 if unit1 is None:
65 source = unit2.source
66 unit1 = store1.findunit(source)
67
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
77 unit1.merge(unit2, overwrite=True, comments=mergecomments)
78 return store1
79
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
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
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