fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / txt2po.py
blobd16ab2a55703d6036e86f234983b797a8f378e93
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Copyright 2004-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 plain text (.txt) files to Gettext PO localization files
25 See: http://translate.sourceforge.net/wiki/toolkit/txt2po for examples and
26 usage instructions
27 """
29 from translate.storage import txt
30 from translate.storage import po
32 class txt2po:
33 def __init__(self, duplicatestyle="msgctxt"):
34 self.duplicatestyle = duplicatestyle
36 def convertstore(self, thetxtfile):
37 """converts a file to .po format"""
38 thetargetfile = po.pofile()
39 targetheader = thetargetfile.makeheader(charset="UTF-8", encoding="8bit")
40 targetheader.addnote("extracted from %s" % thetxtfile.filename, "developer")
41 thetargetfile.addunit(targetheader)
42 for txtunit in thetxtfile.units:
43 newunit = thetargetfile.addsourceunit(txtunit.source)
44 newunit.addlocations(txtunit.getlocations())
45 thetargetfile.removeduplicates(self.duplicatestyle)
46 return thetargetfile
48 def converttxt(inputfile, outputfile, templates, duplicatestyle="msgctxt", encoding="utf-8", flavour=None):
49 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
50 inputstore = txt.TxtFile(inputfile, encoding=encoding, flavour=flavour)
51 convertor = txt2po(duplicatestyle=duplicatestyle)
52 outputstore = convertor.convertstore(inputstore)
53 if outputstore.isempty():
54 return 0
55 outputfile.write(str(outputstore))
56 return 1
58 def main(argv=None):
59 from translate.convert import convert
60 from translate.misc import stdiotell
61 import sys
62 sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
63 formats = {"txt":("po", converttxt), "*":("po", converttxt)}
64 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
65 parser.add_option("", "--encoding", dest="encoding", default='utf-8', type="string",
66 help="The encoding of the input file (default: UTF-8)")
67 parser.passthrough.append("encoding")
68 parser.add_option("", "--flavour", dest="flavour", default="plain",
69 type="choice", choices=["plain", "dokuwiki", "mediawiki"],
70 help="The flavour of text file: plain (default), dokuwiki, mediawiki",
71 metavar="FLAVOUR")
72 parser.passthrough.append("flavour")
73 parser.add_duplicates_option()
74 parser.run(argv)