/usr/share/pyshared/translate/convert/po2csv.py is in translate-toolkit 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2003-2006 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
"""Convert Gettext PO localization files to Comma-Separated Value (.csv) files.
See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/csv2po.html
for examples and usage instructions.
"""
from translate.storage import po
from translate.storage import csvl10n
class po2csv:
def convertcomments(self, inputunit):
return " ".join(inputunit.getlocations())
def convertunit(self, inputunit):
csvunit = csvl10n.csvunit()
if inputunit.isheader():
return None
#csvunit.location = "location"
#csvunit.source = "source"
#csvunit.target = "target"
elif inputunit.isblank():
return None
else:
csvunit.location = self.convertcomments(inputunit)
csvunit.source = inputunit.source
csvunit.target = inputunit.target
return csvunit
def convertplurals(self, inputunit):
"""Convert PO plural units
We only convert the first plural form. So languages with multiple
plurals are not handled. For single plural languages we simply
skip this plural extraction.
"""
if len(inputunit.target.strings) == 1: # No plural forms
return None
csvunit = csvl10n.csvunit()
csvunit.location = self.convertcomments(inputunit)
csvunit.source = inputunit.source.strings[1]
csvunit.target = inputunit.target.strings[1]
return csvunit
def convertstore(self, inputstore, columnorder=None):
if columnorder is None:
columnorder = ['location', 'source', 'target']
outputstore = csvl10n.csvfile(fieldnames=columnorder)
for inputunit in inputstore.units:
outputunit = self.convertunit(inputunit)
if outputunit is not None:
outputstore.addunit(outputunit)
if inputunit.hasplural():
outputunit = self.convertplurals(inputunit)
if outputunit is not None:
outputstore.addunit(outputunit)
return outputstore
def convertcsv(inputfile, outputfile, templatefile, columnorder=None):
"""reads in inputfile using po, converts using po2csv, writes to outputfile"""
# note that templatefile is not used, but it is required by the converter...
inputstore = po.pofile(inputfile)
if inputstore.isempty():
return 0
convertor = po2csv()
outputstore = convertor.convertstore(inputstore, columnorder)
outputfile.write(str(outputstore))
return 1
def main(argv=None):
from translate.convert import convert
formats = {"po": ("csv", convertcsv)}
parser = convert.ConvertOptionParser(formats, description=__doc__)
parser.add_option("", "--columnorder", dest="columnorder", default=None,
help="specify the order and position of columns (location,source,target)")
parser.passthrough.append("columnorder")
parser.run(argv)
if __name__ == '__main__':
main()
|