1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """convert Gettext PO localization files to plain text (.txt) files
24
25 see: http://translate.sourceforge.net/wiki/toolkit/po2txt for examples and
26 usage instructions
27 """
28
29 try:
30 import textwrap
31 except ImportError:
32 textwrap = None
33
34 from translate.storage import factory
35
36
38 """po2txt can take a po file and generate txt. best to give it a template file otherwise will just concat msgstrs"""
39
42
44 """rewraps text as required"""
45 if self.wrap is None:
46 return message
47 return "\n".join([textwrap.fill(line, self.wrap, replace_whitespace=False) for line in message.split("\n")])
48
60
61 - def mergestore(self, inputstore, templatetext, includefuzzy):
62 """converts a file to txt format"""
63 txtresult = templatetext
64
65
66 for unit in inputstore.units:
67 if unit.isheader():
68 continue
69 if not unit.isfuzzy() or includefuzzy:
70 txtsource = unit.source
71 txttarget = self.wrapmessage(unit.target)
72 if unit.istranslated():
73 txtresult = txtresult.replace(txtsource, txttarget)
74 return txtresult
75
76
77 -def converttxt(inputfile, outputfile, templatefile, wrap=None, includefuzzy=False, encoding='utf-8'):
88
89
91 from translate.convert import convert
92 from translate.misc import stdiotell
93 import sys
94 sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
95 formats = {("po", "txt"): ("txt", converttxt), ("po"): ("txt", converttxt), ("xlf", "txt"): ("txt", converttxt), ("xlf"): ("txt", converttxt)}
96 parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
97 parser.add_option("", "--encoding", dest="encoding", default='utf-8', type="string",
98 help="The encoding of the template file (default: UTF-8)")
99 parser.passthrough.append("encoding")
100 if textwrap is not None:
101 parser.add_option("-w", "--wrap", dest="wrap", default=None, type="int",
102 help="set number of columns to wrap text at", metavar="WRAP")
103 parser.passthrough.append("wrap")
104 parser.add_fuzzy_option()
105 parser.run(argv)
106
107
108 if __name__ == '__main__':
109 main()
110