2 # -*- coding: utf-8 -*-
4 # Copyright 2005, 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
23 """convert Gettext PO localization files to XLIFF localization files
25 see: http://translate.sourceforge.net/wiki/toolkit/po2xliff for examples and
29 from translate
.storage
import po
30 from translate
.storage
import poxliff
33 def convertunit(self
, outputstore
, inputunit
, filename
):
34 """creates a transunit node"""
35 source
= inputunit
.source
36 target
= inputunit
.target
37 if inputunit
.isheader():
38 unit
= outputstore
.addheaderunit(target
, filename
)
40 unit
= outputstore
.addsourceunit(source
, filename
, True)
42 #Explicitly marking the fuzzy state will ensure that normal (translated)
43 #units in the PO file end up as approved in the XLIFF file.
45 unit
.markfuzzy(inputunit
.isfuzzy())
47 unit
.markapproved(False)
49 #Handle #: location comments
50 for location
in inputunit
.getlocations():
51 unit
.createcontextgroup("po-reference", self
.contextlist(location
), purpose
="location")
53 #Handle #. automatic comments
54 comment
= inputunit
.getnotes("developer")
56 unit
.createcontextgroup("po-entry", [("x-po-autocomment", comment
)], purpose
="information")
57 unit
.addnote(comment
, origin
="developer")
62 #Handle # other comments
63 comment
= inputunit
.getnotes("translator")
65 unit
.createcontextgroup("po-entry", [("x-po-trancomment", comment
)], purpose
="information")
66 unit
.addnote(comment
, origin
="po-translator")
70 def contextlist(self
, location
):
73 sourcefile
, linenumber
= location
.split(":", 1)
75 sourcefile
, linenumber
= location
, None
76 contexts
.append(("sourcefile", sourcefile
))
78 contexts
.append(("linenumber", linenumber
))
81 def convertstore(self
, inputstore
, templatefile
=None, **kwargs
):
82 """converts a .po file to .xlf format"""
83 if templatefile
is None:
84 outputstore
= poxliff
.PoXliffFile(**kwargs
)
86 outputstore
= poxliff
.PoXliffFile(templatefile
, **kwargs
)
87 filename
= inputstore
.filename
88 for inputunit
in inputstore
.units
:
89 if inputunit
.isblank():
91 transunitnode
= self
.convertunit(outputstore
, inputunit
, filename
)
92 return str(outputstore
)
94 def convertpo(inputfile
, outputfile
, templatefile
):
95 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
96 inputstore
= po
.pofile(inputfile
)
97 if inputstore
.isempty():
99 convertor
= po2xliff()
100 outputstring
= convertor
.convertstore(inputstore
, templatefile
)
101 outputfile
.write(outputstring
)
105 from translate
.convert
import convert
106 formats
= {"po": ("xlf", convertpo
), ("po", "xlf"): ("xlf", convertpo
)}
107 parser
= convert
.ConvertOptionParser(formats
, usepots
=True, usetemplates
=True, description
=__doc__
)
110 if __name__
== '__main__':