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 from translate.storage import po
26 from translate.misc.multistring import multistring
27
30 """Converts a given .po file (PHP Format) to a Python format .po file, the difference being
31 how variable substitutions work. PHP uses a %1$s format, and Python uses
32 a {0} format (zero indexed). This method will convert, e.g.:
33 I have %2$s apples and %1$s oranges
34 to
35 I have {1} apples and {0} oranges
36 This method ignores strings with %s as both languages will recognize that.
37 """
38 thetargetfile = po.pofile(inputfile="")
39
40 for unit in inputstore.units:
41 newunit = self.convertunit(unit)
42 thetargetfile.addunit(newunit)
43 return thetargetfile
44
54
56 return re.sub('%(\d)\$s', lambda x: "{%d}" % (int(x.group(1))-1), input)
57
59 if isinstance(input, multistring):
60 strings = input.strings
61 elif isinstance(input, list):
62 strings = input
63 else:
64 return self.convertstring(input)
65 for index, string in enumerate(strings):
66 strings[index] = re.sub('%(\d)\$s', lambda x: "{%d}" % (int(x.group(1))-1), string)
67 return multistring(strings)
68
70 """Converts from PHP .po format to Python .po format
71
72 @param inputfile: file handle of the source
73 @param outputfile: file handle to write to
74 @param template: unused
75 """
76 convertor = phppo2pypo()
77 inputstore = po.pofile(inputfile)
78 outputstore = convertor.convertstore(inputstore)
79 if outputstore.isempty():
80 return False
81 outputfile.write(str(outputstore))
82 return True
83
85 """Converts PHP .po files to Python .po files."""
86 from translate.convert import convert
87
88 formats = {"po":("po", convertphp2py)}
89 parser = convert.ConvertOptionParser(formats, description=__doc__)
90 parser.run(argv)
91
92 if __name__ == '__main__':
93 main()
94