for git v1.5.2 (and below): chdir to the directory of the target file before executin...
[translate_toolkit.git] / convert / po2csv.py
blob034a2e2d40d19e48503915369f5cb4f24dc15268
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2003-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
22 """convert Gettext PO localization files to Comma-Separated Value (.csv) files
24 see: http://translate.sourceforge.net/wiki/toolkit/po2csv for examples and
25 usage instructions
26 """
28 from translate.storage import po
29 from translate.storage import csvl10n
31 class po2csv:
32 def convertcomments(self, inputunit):
33 return " ".join(inputunit.getlocations())
35 def convertunit(self, inputunit):
36 csvunit = csvl10n.csvunit()
37 if inputunit.isheader():
38 csvunit.comment = "comment"
39 csvunit.source = "original"
40 csvunit.target = "translation"
41 elif inputunit.isblank():
42 return None
43 else:
44 csvunit.comment = self.convertcomments(inputunit)
45 csvunit.source = inputunit.source.strings[0]
46 csvunit.target = inputunit.target.strings[0]
47 return csvunit
49 def convertplurals(self, inputunit):
50 csvunit = csvl10n.csvunit()
51 csvunit.comment = self.convertcomments(inputunit)
52 csvunit.source = inputunit.source.strings[1]
53 csvunit.target = inputunit.target.strings[1]
54 return csvunit
56 def convertstore(self, inputstore, columnorder=None):
57 outputstore = csvl10n.csvfile(fieldnames=columnorder)
58 for inputunit in inputstore.units:
59 outputunit = self.convertunit(inputunit)
60 if outputunit is not None:
61 outputstore.addunit(outputunit)
62 if inputunit.hasplural():
63 outputunit = self.convertplurals(inputunit)
64 if outputunit is not None:
65 outputstore.addunit(outputunit)
66 return outputstore
68 def convertcsv(inputfile, outputfile, templatefile, columnorder=None):
69 """reads in inputfile using po, converts using po2csv, writes to outputfile"""
70 # note that templatefile is not used, but it is required by the converter...
71 inputstore = po.pofile(inputfile)
72 if inputstore.isempty():
73 return 0
74 convertor = po2csv()
75 outputstore = convertor.convertstore(inputstore, columnorder)
76 outputfile.write(str(outputstore))
77 return 1
79 def main(argv=None):
80 from translate.convert import convert
81 formats = {"po":("csv", convertcsv)}
82 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
83 parser.add_option("", "--columnorder", dest="columnorder", default=None,
84 help="specify the order and position of columns (comment,source,target)")
85 parser.passthrough.append("columnorder")
86 parser.run(argv)
89 if __name__ == '__main__':
90 main()