fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / xliff2po.py
blob2ec2a11f2fe3e36f0151d71b9007405d82004701
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2002-2006 Zuza Software Foundation
5 #
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
26 usage instructions
27 """
29 from translate.storage import po
30 from translate.storage import xliff
31 from translate.misc import wStringIO
33 class xliff2po:
34 def converttransunit(self, transunit):
35 """makes a pounit from the given transunit"""
36 thepo = po.pounit()
38 #Header
39 if transunit.getrestype() == "x-gettext-domain-header":
40 thepo.source = ""
41 else:
42 thepo.source = transunit.source
43 thepo.target = transunit.target
45 #Location comments
46 locations = transunit.getlocations()
47 if locations:
48 thepo.addlocation("%s" % " ".join(locations))
50 #NOTE: Supporting both <context> and <note> tags in xliff files for comments
51 #Translator comments
52 trancomments = transunit.getnotes("translator")
53 if trancomments:
54 thepo.addnote(trancomments, origin="translator")
56 #Automatic and Developer comments
57 autocomments = transunit.getnotes("developer")
58 if autocomments:
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():
63 thepo.markfuzzy(True)
65 return thepo
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)
81 return thetargetfile
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():
88 return 0
89 outputfile.write(str(outputstore))
90 return 1
92 def main(argv=None):
93 from translate.convert import convert
94 formats = {"xlf":("po", convertxliff)}
95 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
96 parser.run(argv)