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
23 """convert XLIFF localization files to Gettext PO localization files
25 see: http://translate.sourceforge.net/wiki/toolkit/xliff2po for examples and
29 from translate
.storage
import po
30 from translate
.storage
import xliff
31 from translate
.misc
import wStringIO
34 def converttransunit(self
, transunit
):
35 """makes a pounit from the given transunit"""
39 if transunit
.getrestype() == "x-gettext-domain-header":
42 thepo
.source
= transunit
.source
43 thepo
.target
= transunit
.target
46 locations
= transunit
.getlocations()
48 thepo
.addlocation("%s" % " ".join(locations
))
50 #NOTE: Supporting both <context> and <note> tags in xliff files for comments
52 trancomments
= transunit
.getnotes("translator")
54 thepo
.addnote(trancomments
, origin
="translator")
56 #Automatic and Developer comments
57 autocomments
= transunit
.getnotes("developer")
59 thepo
.addnote(autocomments
, origin
="developer")
61 #See 5.6.1 of the spec. We should not check fuzzyness, but approved attribute
62 if transunit
.isfuzzy():
67 def convertstore(self
, inputfile
):
68 """converts a .xliff file to .po format"""
69 # XXX: The inputfile is converted to string because Pootle supplies
70 # XXX: a PootleFile object as input which cannot be sent to PoXliffFile.
71 # XXX: The better way would be to have a consistent conversion API.
72 if not isinstance(inputfile
, (file, wStringIO
.StringIO
)):
73 inputfile
= str(inputfile
)
74 XliffFile
= xliff
.xlifffile
.parsestring(inputfile
)
75 thetargetfile
= po
.pofile()
76 targetheader
= thetargetfile
.makeheader(charset
="UTF-8", encoding
="8bit")
77 # TODO: support multiple files
78 for transunit
in XliffFile
.units
:
79 thepo
= self
.converttransunit(transunit
)
80 thetargetfile
.addunit(thepo
)
83 def convertxliff(inputfile
, outputfile
, templates
):
84 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
85 convertor
= xliff2po()
86 outputstore
= convertor
.convertstore(inputfile
)
87 if outputstore
.isempty():
89 outputfile
.write(str(outputstore
))
93 from translate
.convert
import convert
94 formats
= {"xlf":("po", convertxliff
)}
95 parser
= convert
.ConvertOptionParser(formats
, usepots
=True, description
=__doc__
)