1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Builds a new translation file with the target of the input language as
23 source language.
24
25 Ensure that the two po files correspond 100% to the same pot file before using
26 this.
27
28 To translate Kurdish (ku) through French::
29 po2swap -i fr/ -t ku -o fr-ku
30
31 To convert the fr-ku files back to en-ku::
32 po2swap --reverse -i fr/ -t fr-ku -o en-ku
33
34 See: http://translate.sourceforge.net/wiki/toolkit/poswap for further examples and
35 usage instructions
36 """
37
38 from translate.storage import po
39 from translate.convert import convert
40
41
51
52
53 -def convertpo(inputpofile, outputpotfile, template, reverse=False):
54 """reads in inputpofile, removes the header, writes to outputpotfile."""
55 inputpo = po.pofile(inputpofile)
56 templatepo = po.pofile(template)
57 if reverse:
58 swapdir(inputpo)
59 templatepo.makeindex()
60 header = inputpo.header()
61 if header:
62 inputpo.units = inputpo.units[1:]
63
64 for i, unit in enumerate(inputpo.units):
65 for location in unit.getlocations():
66 templateunit = templatepo.locationindex.get(location, None)
67 if templateunit and templateunit.source == unit.source:
68 break
69 else:
70 templateunit = templatepo.findunit(unit.source)
71
72 unit.othercomments = []
73 if unit.target and not unit.isfuzzy():
74 unit.source = unit.target
75 elif not reverse:
76 if inputpo.filename:
77 unit.addnote("No translation found in %s" % inputpo.filename, origin="programmer")
78 else:
79 unit.addnote("No translation found in the supplied source language", origin="programmer")
80 unit.target = ""
81 unit.markfuzzy(False)
82 if templateunit:
83 unit.addnote(templateunit.getnotes(origin="translator"))
84 unit.markfuzzy(templateunit.isfuzzy())
85 unit.target = templateunit.target
86 if unit.isobsolete():
87 del inputpo.units[i]
88 outputpotfile.write(str(inputpo))
89 return 1
90
91
93 formats = {("po", "po"): ("po", convertpo), ("po", "pot"): ("po", convertpo), "po": ("po", convertpo)}
94 parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
95 parser.add_option("", "--reverse", dest="reverse", default=False, action="store_true",
96 help="reverse the process of intermediate language conversion")
97 parser.passthrough.append("reverse")
98 parser.run(argv)
99
100
101 if __name__ == '__main__':
102 main()
103