1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """ Convert Python format .po files to PHP format .po files """
23
24 import re
25
26 from translate.storage import po
27 from translate.misc.multistring import multistring
28
29
31
33 """Converts a given .po file (Python Format) to a PHP format .po file, the difference being
34 how variable substitutions work. PHP uses a %1$s format, and Python uses
35 a {0} format (zero indexed). This method will convert, e.g.:
36 I have {1} apples and {0} oranges
37 to
38 I have %2$s apples and %1$s oranges
39 This method ignores strings with %s as both languages will recognize that.
40 """
41 thetargetfile = po.pofile(inputfile="")
42
43 for unit in inputstore.units:
44 newunit = self.convertunit(unit)
45 thetargetfile.addunit(newunit)
46 return thetargetfile
47
57
59 return re.sub('\{(\d)\}', lambda x: "%%%d$s" % (int(x.group(1))+1), string)
60
62 if isinstance(input, multistring):
63 strings = input.strings
64 elif isinstance(input, list):
65 strings = input
66 else:
67 return self.convertstring(input)
68
69 for index, string in enumerate(strings):
70 strings[index] = re.sub('\{(\d)\}', lambda x: "%%%d$s" % (int(x.group(1))+1), string)
71 return strings
72
73
75 """Converts from Python .po to PHP .po
76
77 @param inputfile: file handle of the source
78 @param outputfile: file handle to write to
79 @param template: unused
80 """
81 convertor = pypo2phppo()
82 inputstore = po.pofile(inputfile)
83 outputstore = convertor.convertstore(inputstore)
84 if outputstore.isempty():
85 return False
86 outputfile.write(str(outputstore))
87 return True
88
89
91 """Converts from Python .po to PHP .po"""
92 from translate.convert import convert
93
94 formats = {"po": ("po", convertpy2php)}
95 parser = convert.ConvertOptionParser(formats, description=__doc__)
96 parser.run(argv)
97
98
99 if __name__ == '__main__':
100 main()
101