2 # -*- coding: utf-8 -*-
4 # Copyright 2002-2006 Zuza Software Foundation
6 # This file is part of translate.
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.
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.
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
22 """Merges XLIFF and Gettext PO localization files
24 Snippet file produced by pogrep or updated by a translator can be merged into
27 See: http://translate.sourceforge.net/wiki/toolkit/pomerge for examples and
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
37 def mergestores(store1
, store2
, mergeblanks
, mergecomments
):
38 """Take any new translations in store2 and write them into store1."""
40 for unit2
in store2
.units
:
42 if isinstance(store1
, poheader
):
43 store1
.mergeheaders(store2
)
46 # there may be more than one entity due to msguniq merge
47 entities
= unit2
.getlocations()
48 if len(entities
) == 0:
51 if source
in store1
.sourceindex
:
52 unit1
= store1
.sourceindex
[source
]
54 sys
.stderr
.write(str(unit2
) + "\n")
56 # finally set the new definition in unit1
57 unit1
.merge(unit2
, overwrite
=True)
58 for entity
in entities
:
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:
67 # if locationindex was not unique, use the sourceindex
70 if source
in store1
.sourceindex
:
71 unit1
= store1
.sourceindex
[source
]
72 # check if we found a matching po element
74 print >> sys
.stderr
, "# the following po element was not found"
75 sys
.stderr
.write(str(unit2
) + "\n")
79 if len(target
.strip()) == 0: continue
80 # finally set the new definition in unit1
81 unit1
.merge(unit2
, overwrite
=True, comments
=mergecomments
)
85 """Convert a string value to boolean
87 @param option: yes, true, 1, no, false, 0
92 option
= option
.lower()
93 if option
in ("yes", "true", "1"):
95 elif option
in ("no", "false", "0"):
98 raise ValueError("invalid boolean value: %r" % option
)
100 def mergestore(inputfile
, outputfile
, templatefile
, mergeblanks
="no", mergecomments
="yes"):
102 mergecomments
= str2bool(mergecomments
)
104 raise ValueError("invalid mergecomments value: %r" % mergecomments
)
106 mergeblanks
= str2bool(mergeblanks
)
108 raise ValueError("invalid mergeblanks value: %r" % mergeblanks
)
109 inputstore
= factory
.getobject(inputfile
)
110 if templatefile
is None:
112 templatestore
= type(inputstore
)()
114 templatestore
= factory
.getobject(templatefile
)
115 templatestore
.makeindex()
116 inputstore
.makeindex()
117 outputstore
= mergestores(templatestore
, inputstore
, mergeblanks
, mergecomments
)
118 if outputstore
.isempty():
120 outputfile
.write(str(outputstore
))
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")
144 if __name__
== '__main__':