1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Fill localization files with suggested translations based on
22 translation memory and existing translations.
23 """
24
25 from translate.storage import factory
26 from translate.storage import xliff
27 from translate.search import match
28
29
30 tmmatcher = None
31
32 -def memory(tmfiles, max_candidates=1, min_similarity=75, max_length=1000):
33 """Returns the TM store to use. Only initialises on first call."""
34 global tmmatcher
35
36 if tmmatcher is None:
37 if isinstance(tmfiles, list):
38 tmstore = [factory.getobject(tmfile) for tmfile in tmfiles]
39 else:
40 tmstore = factory.getobject(tmfiles)
41 tmmatcher = match.matcher(tmstore, max_candidates=max_candidates, min_similarity=min_similarity, max_length=max_length)
42 return tmmatcher
43
44
45 -def pretranslate_file(input_file, output_file, template_file, tm=None, min_similarity=75, fuzzymatching=True):
46 """Pretranslate any factory supported file with old translations and translation memory."""
47 input_store = factory.getobject(input_file)
48 template_store = None
49 if template_file is not None:
50 template_store = factory.getobject(template_file)
51
52 output = pretranslate_store(input_store, template_store, tm, min_similarity, fuzzymatching)
53 output_file.write(str(output))
54 return 1
55
56
58 """Returns a matching unit from a template."""
59
60
61
62 locations = input_unit.getlocations()
63 if not locations or ":" in locations[0]:
64
65 matching_unit = template_store.findid(input_unit.getid())
66 return matching_unit
67
68 else:
69
70
71
72 for location in locations:
73 matching_unit = template_store.locationindex.get(location, None)
74
75 if matching_unit is not None and matching_unit.source == input_unit.source and matching_unit.gettargetlen() > 0:
76 return matching_unit
77
78
80 """Return a fuzzy match from a queue of matchers."""
81 for matcher in matchers:
82 fuzzycandidates = matcher.matches(input_unit.source)
83 if fuzzycandidates:
84 return fuzzycandidates[0]
85
86
88 """Pretranslate a unit or return unchanged if no translation was found."""
89
90 matching_unit = None
91
92 if template_store:
93 matching_unit = match_template_id(input_unit, template_store)
94
95 if matching_unit and matching_unit.gettargetlen() > 0:
96 input_unit.merge(matching_unit, authoritative=True)
97 elif matchers:
98
99 matching_unit = match_fuzzy(input_unit, matchers)
100 if matching_unit and matching_unit.gettargetlen() > 0:
101
102 if isinstance(input_unit, xliff.xliffunit):
103
104 input_unit.addalttrans(matching_unit.target, origin="fish", sourcetxt=matching_unit.source)
105 else:
106 input_unit.merge(matching_unit, authoritative=True)
107
108
109
110 if mark_reused and matching_unit and template_store:
111 original_unit = template_store.findunit(matching_unit.source)
112 if original_unit is not None:
113 original_unit.reused = True
114
115 return input_unit
116
118 """PO format specific template preparation logic."""
119
120 for unit in template_store.units:
121 if unit.isobsolete():
122 unit.resurrect()
123
124 -def pretranslate_store(input_store, template_store, tm=None, min_similarity=75, fuzzymatching=True):
125 """Do the actual pretranslation of a whole store."""
126
127 matchers = []
128
129 if template_store is not None:
130 template_store.makeindex()
131
132 prepare_template = "prepare_template_%s" % template_store.__class__.__name__
133 if globals().has_key(prepare_template):
134 globals()[prepare_template](template_store)
135
136 if fuzzymatching:
137
138
139 matcher = match.matcher(template_store, max_candidates=1, min_similarity=min_similarity, max_length=3000, usefuzzy=True)
140 matcher.addpercentage = False
141 matchers.append(matcher)
142
143
144
145 if tm and fuzzymatching:
146
147 matcher = memory(tm, max_candidates=1, min_similarity=min_similarity, max_length=1000)
148 matcher.addpercentage = False
149 matchers.append(matcher)
150
151
152 for input_unit in input_store.units:
153 if input_unit.istranslatable():
154 input_unit = pretranslate_unit(input_unit, template_store, matchers)
155
156 return input_store
157
158
159 -def main(argv=None):
160 from translate.convert import convert
161 formats = {"pot": ("po", pretranslate_file), ("pot", "po"): ("po", pretranslate_file),
162 "po": ("po", pretranslate_file), ("po", "po"): ("po", pretranslate_file),
163 "xlf": ("xlf", pretranslate_file), ("xlf", "xlf"): ("xlf", pretranslate_file),
164 }
165 parser = convert.ConvertOptionParser(formats, usetemplates=True,
166 allowmissingtemplate=True, description=__doc__)
167 parser.add_option("", "--tm", dest="tm", default=None,
168 help="The file to use as translation memory when fuzzy matching")
169 parser.passthrough.append("tm")
170 defaultsimilarity = 75
171 parser.add_option("-s", "--similarity", dest="min_similarity", default=defaultsimilarity,
172 type="float", help="The minimum similarity for inclusion (default: %d%%)" % defaultsimilarity)
173 parser.passthrough.append("min_similarity")
174 parser.add_option("--nofuzzymatching", dest="fuzzymatching", action="store_false",
175 default=True, help="Disable fuzzy matching")
176 parser.passthrough.append("fuzzymatching")
177 parser.run(argv)
178
179
180 if __name__ == '__main__':
181 main()
182