1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Segment Gettext PO, XLIFF and TMX localization files at the sentence level.
22
23 See: http://translate.sourceforge.net/wiki/toolkit/posegment for examples and
24 usage instructions
25 """
26
27 from translate.storage import factory
28 from translate.lang import factory as lang_factory
29
31
32 - def __init__(self, sourcelang, targetlang, stripspaces=True):
36
38 if unit.isheader() or unit.hasplural():
39 return [unit]
40 sourcesegments = self.sourcelang.sentences(unit.source, strip=self.stripspaces)
41 targetsegments = self.targetlang.sentences(unit.target, strip=self.stripspaces)
42 if unit.istranslated() and (len(sourcesegments) != len(targetsegments)):
43 return [unit]
44
45
46
47 units = []
48 for i in range(len(sourcesegments)):
49 newunit = unit.copy()
50 newunit.source = sourcesegments[i]
51 if not unit.istranslated():
52 newunit.target = ""
53 else:
54 newunit.target = targetsegments[i]
55 units.append(newunit)
56 return units
57
59 tostore = type(fromstore)()
60 for unit in fromstore.units:
61 newunits = self.segmentunit(unit)
62 for newunit in newunits:
63 tostore.addunit(newunit)
64 return tostore
65
66 -def segmentfile(inputfile, outputfile, templatefile, sourcelanguage="en", targetlanguage=None, stripspaces=True):
78
80 from translate.convert import convert
81 formats = {"po":("po", segmentfile), "xlf":("xlf", segmentfile), "tmx": ("tmx", segmentfile)}
82 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
83 parser.add_option("-l", "--language", dest="targetlanguage", default=None,
84 help="the target language code", metavar="LANG")
85 parser.add_option("", "--source-language", dest="sourcelanguage", default=None,
86 help="the source language code (default 'en')", metavar="LANG")
87 parser.passthrough.append("sourcelanguage")
88 parser.passthrough.append("targetlanguage")
89 parser.add_option("", "--keepspaces", dest="stripspaces", action="store_false",
90 default=True, help="Disable automatic stripping of whitespace")
91 parser.passthrough.append("stripspaces")
92 parser.run()
93
94
95 if __name__ == '__main__':
96 main()
97