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
30
32
33 - def __init__(self, sourcelang, targetlang, stripspaces=True):
37
39 if unit.isheader() or unit.hasplural():
40 return [unit]
41 sourcesegments = self.sourcelang.sentences(unit.source, strip=self.stripspaces)
42 targetsegments = self.targetlang.sentences(unit.target, strip=self.stripspaces)
43 if unit.istranslated() and (len(sourcesegments) != len(targetsegments)):
44 return [unit]
45
46
47
48 units = []
49 for i in range(len(sourcesegments)):
50 newunit = unit.copy()
51 newunit.source = sourcesegments[i]
52 if not unit.istranslated():
53 newunit.target = ""
54 else:
55 newunit.target = targetsegments[i]
56 units.append(newunit)
57 return units
58
60 tostore = type(fromstore)()
61 for unit in fromstore.units:
62 newunits = self.segmentunit(unit)
63 for newunit in newunits:
64 tostore.addunit(newunit)
65 return tostore
66
67
68 -def segmentfile(inputfile, outputfile, templatefile, sourcelanguage="en", targetlanguage=None, stripspaces=True):
80
81
83 from translate.convert import convert
84 formats = {"po": ("po", segmentfile), "xlf": ("xlf", segmentfile), "tmx": ("tmx", segmentfile)}
85 parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
86 parser.add_option("-l", "--language", dest="targetlanguage", default=None,
87 help="the target language code", metavar="LANG")
88 parser.add_option("", "--source-language", dest="sourcelanguage", default=None,
89 help="the source language code (default 'en')", metavar="LANG")
90 parser.passthrough.append("sourcelanguage")
91 parser.passthrough.append("targetlanguage")
92 parser.add_option("", "--keepspaces", dest="stripspaces", action="store_false",
93 default=True, help="Disable automatic stripping of whitespace")
94 parser.passthrough.append("stripspaces")
95 parser.run()
96
97
98 if __name__ == '__main__':
99 main()
100