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 from translate.storage import factory
30 try:
31 import textwrap
32 except:
33 textwrap = None
34
36 """po2txt can take a po file and generate txt. best to give it a template file otherwise will just concat msgstrs"""
39
41 """rewraps text as required"""
42 if self.wrap is None:
43 return message
44 return "\n".join([textwrap.fill(line, self.wrap, replace_whitespace=False) for line in message.split("\n")])
45
57
58 - def mergestore(self, inputstore, templatetext, includefuzzy):
59 """converts a file to txt format"""
60 txtresult = templatetext
61
62
63 for unit in inputstore.units:
64 if unit.isheader():
65 continue
66 if not unit.isfuzzy() or includefuzzy:
67 txtsource = unit.source
68 txttarget = self.wrapmessage(unit.target)
69 if unit.istranslated():
70 txtresult = txtresult.replace(txtsource, txttarget)
71 return txtresult
72
73 -def converttxt(inputfile, outputfile, templatefile, wrap=None, includefuzzy=False, encoding='utf-8'):
84
86 from translate.convert import convert
87 from translate.misc import stdiotell
88 import sys
89 sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
90 formats = {("po", "txt"):("txt", converttxt), ("po"):("txt", converttxt), ("xlf", "txt"):("txt", converttxt), ("xlf"):("txt", converttxt)}
91 parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
92 parser.add_option("", "--encoding", dest="encoding", default='utf-8', type="string",
93 help="The encoding of the template file (default: UTF-8)")
94 parser.passthrough.append("encoding")
95 if textwrap is not None:
96 parser.add_option("-w", "--wrap", dest="wrap", default=None, type="int",
97 help="set number of columns to wrap text at", metavar="WRAP")
98 parser.passthrough.append("wrap")
99 parser.add_fuzzy_option()
100 parser.run(argv)
101
102
103 if __name__ == '__main__':
104 main()
105