1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """convert plain text (.txt) files to Gettext PO localization files
24
25 See: http://translate.sourceforge.net/wiki/toolkit/txt2po for examples and
26 usage instructions
27 """
28
29 from translate.storage import txt
30 from translate.storage import po
31
33 - def __init__(self, duplicatestyle="msgctxt"):
34 self.duplicatestyle = duplicatestyle
35
37 """converts a file to .po format"""
38 thetargetfile = po.pofile()
39 targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
40 targetheader.addnote("extracted from %s" % thetxtfile.filename, "developer")
41
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
47
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
57
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)
75