1#
2# Copyright 2006-2007 Zuza Software Foundation
3#
4# This file is part of translate.
5#
6# translate is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# translate is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, see <http://www.gnu.org/licenses/>.
18
19"""Convert TermBase eXchange (.tbx) glossary file into a Gettext PO file
20
21See: http://docs.translatehouse.org/projects/translate-toolkit/en/latest/commands/tbx2po.html
22for examples and usage instructions
23"""
24
25from translate.storage import po, tbx
26
27
28class tbx2po:
29    """A class that takes translations from a .tbx file and puts them in a .po
30    file
31    """
32
33    def convertfile(self, tbxfile):
34        """Converts a tbxfile to a tbxfile, and returns it. uses templatepo if
35        given at construction
36        """
37        self.pofile = po.pofile()
38        for tbxunit in tbxfile.units:
39            term = po.pounit()
40            term.source = tbxunit.source
41            term.target = tbxunit.target
42            term.setcontext(tbxunit.getnotes("definition"))
43            term.addnote("Part of speech: %s" % tbxunit.getnotes("pos"), "developer")
44            self.pofile.addunit(term)
45        self.pofile.removeduplicates()
46        return self.pofile
47
48
49def converttbx(inputfile, outputfile, templatefile, charset=None, columnorder=None):
50    """Reads in inputfile using tbx, converts using tbx2po, writes to
51    outputfile
52    """
53    inputstore = tbx.tbxfile(inputfile)
54    convertor = tbx2po()
55    outputstore = convertor.convertfile(inputstore)
56    if len(outputstore.units) == 0:
57        return 0
58    outputstore.serialize(outputfile)
59    return 1
60
61
62def main():
63    from translate.convert import convert
64
65    formats = {
66        ("tbx", None): ("po", converttbx),
67    }
68    parser = convert.ConvertOptionParser(
69        formats, usetemplates=False, description=__doc__
70    )
71    parser.run()
72
73
74if __name__ == "__main__":
75    main()
76