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