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