fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_po2xliff.py
blob32d88bb78258b08d6fa9212c17bf51cc543610ea
1 #!/usr/bin/env python
3 from translate.convert import po2xliff
4 from translate.storage import po
5 from translate.storage import poxliff
6 from translate.storage import lisa
8 class TestPO2XLIFF:
10 def po2xliff(self, posource, sourcelanguage='en', targetlanguage=None):
11 """helper that converts po source to xliff source without requiring files"""
12 postore = po.pofile(posource)
13 convertor = po2xliff.po2xliff()
14 outputxliff = convertor.convertstore(postore, None, sourcelanguage=sourcelanguage, targetlanguage=targetlanguage)
15 return poxliff.PoXliffFile(outputxliff)
17 def getnode(self, xliff):
18 """Retrieves the trans-unit node from the dom"""
19 assert len(xliff.units) == 1
20 unit = xliff.units[0]
21 return unit
23 def test_minimal(self):
24 minipo = '''msgid "red"\nmsgstr "rooi"\n'''
25 xliff = self.po2xliff(minipo)
26 print "The generated xml:"
27 print str(xliff)
28 assert len(xliff.units) == 1
29 assert xliff.translate("red") == "rooi"
30 assert xliff.translate("bla") is None
32 def test_basic(self):
33 minipo = """# Afrikaans translation of program ABC
35 msgid ""
36 msgstr ""
37 "Project-Id-Version: program 2.1-branch\n"
38 "Report-Msgid-Bugs-To: \n"
39 "POT-Creation-Date: 2006-01-09 07:15+0100\n"
40 "PO-Revision-Date: 2004-03-30 17:02+0200\n"
41 "Last-Translator: Zuza Software Foundation <xxx@translate.org.za>\n"
42 "Language-Team: Afrikaans <translate-discuss-xxx@lists.sourceforge.net>\n"
43 "MIME-Version: 1.0\n"
44 "Content-Type: text/plain; charset=UTF-8\n"
45 "Content-Transfer-Encoding: 8bit\n"
47 # Please remember to do something
48 #: ../dir/file.xml.in.h:1 ../dir/file2.xml.in.h:4
49 msgid "Applications"
50 msgstr "Toepassings"
51 """
52 xliff = self.po2xliff(minipo)
53 print "The generated xml:"
54 print str(xliff)
55 assert xliff.translate("Applications") == "Toepassings"
56 assert xliff.translate("bla") is None
57 xmltext = str(xliff)
58 assert xmltext.index('<xliff ') >= 0
59 assert xmltext.index(' version="1.1"') >= 0
60 assert xmltext.index('<file')
61 assert xmltext.index('source-language')
62 assert xmltext.index('datatype')
64 def test_multiline(self):
65 """Test multiline po entry"""
66 minipo = r'''msgid "First part "
67 "and extra"
68 msgstr "Eerste deel "
69 "en ekstra"'''
70 xliff = self.po2xliff(minipo)
71 print "The generated xml:"
72 print str(xliff)
73 assert xliff.translate('First part and extra') == 'Eerste deel en ekstra'
76 def test_escapednewlines(self):
77 """Test the escaping of newlines"""
78 minipo = r'''msgid "First line\nSecond line"
79 msgstr "Eerste lyn\nTweede lyn"
80 '''
81 xliff = self.po2xliff(minipo)
82 print "The generated xml:"
83 xmltext = str(xliff)
84 print xmltext
85 assert xliff.translate("First line\nSecond line") == "Eerste lyn\nTweede lyn"
86 assert xliff.translate("First line\\nSecond line") is None
87 assert xmltext.find("line\\nSecond") == -1
88 assert xmltext.find("lyn\\nTweede") == -1
89 assert xmltext.find("line\nSecond") > 0
90 assert xmltext.find("lyn\nTweede") > 0
92 def test_escapedtabs(self):
93 """Test the escaping of tabs"""
94 minipo = r'''msgid "First column\tSecond column"
95 msgstr "Eerste kolom\tTweede kolom"
96 '''
97 xliff = self.po2xliff(minipo)
98 print "The generated xml:"
99 xmltext = str(xliff)
100 print xmltext
101 assert xliff.translate("First column\tSecond column") == "Eerste kolom\tTweede kolom"
102 assert xliff.translate("First column\\tSecond column") is None
103 assert xmltext.find("column\\tSecond") == -1
104 assert xmltext.find("kolom\\tTweede") == -1
105 assert xmltext.find("column\tSecond") > 0
106 assert xmltext.find("kolom\tTweede") > 0
108 def test_escapedquotes(self):
109 """Test the escaping of quotes (and slash)"""
110 minipo = r'''msgid "Hello \"Everyone\""
111 msgstr "Good day \"All\""
113 msgid "Use \\\"."
114 msgstr "Gebruik \\\"."
116 xliff = self.po2xliff(minipo)
117 print "The generated xml:"
118 xmltext = str(xliff)
119 print xmltext
120 assert xliff.translate('Hello "Everyone"') == 'Good day "All"'
121 assert xliff.translate(r'Use \".') == r'Gebruik \".'
122 assert xmltext.find(r'\&quot;') > 0 or xmltext.find(r'\"') > 0
123 assert xmltext.find(r"\\") == -1
125 def getcontexttuples(self, node, namespace):
126 """Returns all the information in the context nodes as a list of tuples
127 of (type, text)"""
128 contexts = node.findall(".//{%s}context" % namespace)
129 return [(context.get("context-type"), lisa.getText(context)) for context in contexts]
131 def test_locationcomments(self):
132 minipo = r'''#: file.c:123 asdf.c
133 msgid "one"
134 msgstr "kunye"
136 xliff = self.po2xliff(minipo)
137 print "The generated xml:"
138 xmltext = str(xliff)
139 print xmltext
140 assert xliff.translate("one") == "kunye"
141 assert len(xliff.units) == 1
142 node = xliff.units[0].xmlelement
143 contextgroups = node.findall(".//%s" % xliff.namespaced("context-group"))
144 assert len(contextgroups) == 2
145 for group in contextgroups:
146 assert group.get("name") == "po-reference"
147 assert group.get("purpose") == "location"
148 tuples = self.getcontexttuples(node, xliff.namespace)
149 assert tuples == [("sourcefile", "file.c"), ("linenumber", "123"), ("sourcefile", "asdf.c")]
151 def test_othercomments(self):
152 minipo = r'''# Translate?
153 # How?
154 msgid "one"
155 msgstr "kunye"
157 xliff = self.po2xliff(minipo)
158 print "The generated xml:"
159 xmltext = str(xliff)
160 print xmltext
161 assert xliff.translate("one") == "kunye"
162 assert len(xliff.units) == 1
163 node = xliff.units[0].xmlelement
164 contextgroups = node.findall(".//%s" % xliff.namespaced("context-group"))
165 assert len(contextgroups) == 1
166 for group in contextgroups:
167 assert group.get("name") == "po-entry"
168 assert group.get("purpose") == "information"
169 tuples = self.getcontexttuples(node, xliff.namespace)
170 assert tuples == [("x-po-trancomment", "Translate?\nHow?")]
172 assert xliff.units[0].getnotes("translator") == "Translate?\nHow?"
175 def test_automaticcomments(self):
176 minipo = r'''#. Don't translate.
177 #. Please
178 msgid "one"
179 msgstr "kunye"
181 xliff = self.po2xliff(minipo)
182 print "The generated xml:"
183 xmltext = str(xliff)
184 print xmltext
185 assert xliff.translate("one") == "kunye"
186 assert len(xliff.units) == 1
187 node = xliff.units[0].xmlelement
188 contextgroups = node.findall(".//%s" % xliff.namespaced("context-group"))
189 assert len(contextgroups) == 1
190 for group in contextgroups:
191 assert group.get("name") == "po-entry"
192 assert group.get("purpose") == "information"
193 tuples = self.getcontexttuples(node, xliff.namespace)
194 assert tuples == [("x-po-autocomment", "Don't translate.\nPlease")]
196 def test_header(self):
197 minipo = r'''# Pulana Translation for bla
198 # Hallo Ma!
199 #, fuzzy
200 msgid ""
201 msgstr ""
202 "MIME-Version: 1.0\n"
204 xliff = self.po2xliff(minipo)
205 print "The generated xml:"
206 xmltext = str(xliff)
207 print xmltext
208 assert len(xliff.units) == 1
209 unit = xliff.units[0]
210 assert unit.source == unit.target == "MIME-Version: 1.0\n"
211 assert unit.xmlelement.get("restype") == "x-gettext-domain-header"
212 assert unit.xmlelement.get("approved") == "no"
213 assert unit.xmlelement.get("{%s}space" % lisa.XML_NS) == "preserve"
214 assert unit.getnotes("po-translator") == "Pulana Translation for bla\nHallo Ma!"
216 def test_fuzzy(self):
217 minipo = r'''#, fuzzy
218 msgid "two"
219 msgstr "pedi"
221 msgid "three"
222 msgstr "raro"
224 xliff = self.po2xliff(minipo)
225 print "The generated xml:"
226 xmltext = str(xliff)
227 print xmltext
228 assert len(xliff.units) == 2
229 assert xliff.units[0].isfuzzy()
230 assert not xliff.units[1].isfuzzy()
232 def test_germanic_plurals(self):
233 minipo = r'''msgid "cow"
234 msgid_plural "cows"
235 msgstr[0] "inkomo"
236 msgstr[1] "iinkomo"
238 xliff = self.po2xliff(minipo)
239 print "The generated xml:"
240 xmltext = str(xliff)
241 print xmltext
242 assert len(xliff.units) == 1
243 assert xliff.translate("cow") == "inkomo"
245 def test_funny_plurals(self):
246 minipo = r'''msgid "cow"
247 msgid_plural "cows"
248 msgstr[0] "inkomo"
249 msgstr[1] "iinkomo"
250 msgstr[2] "iiinkomo"
252 xliff = self.po2xliff(minipo)
253 print "The generated xml:"
254 xmltext = str(xliff)
255 print xmltext
256 assert len(xliff.units) == 1
257 assert xliff.translate("cow") == "inkomo"
259 def test_language_tags(self):
260 minipo = r'''msgid "Een"
261 msgstr "Uno"
263 xliff = self.po2xliff(minipo, "af", "es")
264 assert xliff.sourcelanguage == "af"
265 assert xliff.targetlanguage == "es"
267 def test_variables(self):
268 minipo = r'''msgid "%s%s%s%s has made %s his or her buddy%s%s"
269 msgstr "%s%s%s%s het %s sy/haar vriend/vriendin gemaak%s%s"'''
270 xliff = self.po2xliff(minipo)
271 print xliff.units[0].source
272 assert xliff.units[0].source == "%s%s%s%s has made %s his or her buddy%s%s"
274 def test_approved(self):
275 minipo = r'''#, fuzzy
276 msgid "two"
277 msgstr "pedi"
279 msgid "three"
280 msgstr "raro"
282 msgid "four"
283 msgstr ""
285 xliff = self.po2xliff(minipo)
286 print "The generated xml:"
287 xmltext = str(xliff)
288 print xmltext
289 assert len(xliff.units) == 3
290 assert xliff.units[0].xmlelement.get("approved") == "no"
291 assert not xliff.units[0].isapproved()
292 assert xliff.units[1].xmlelement.get("approved") == "yes"
293 assert xliff.units[1].isapproved()
294 assert xliff.units[2].xmlelement.get("approved") == "no"
295 assert not xliff.units[2].isapproved()