3 from translate
.convert
import po2xliff
4 from translate
.storage
import po
5 from translate
.storage
import poxliff
6 from translate
.storage
import lisa
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
23 def test_minimal(self
):
24 minipo
= '''msgid "red"\nmsgstr "rooi"\n'''
25 xliff
= self
.po2xliff(minipo
)
26 print "The generated xml:"
28 assert len(xliff
.units
) == 1
29 assert xliff
.translate("red") == "rooi"
30 assert xliff
.translate("bla") is None
33 minipo
= """# Afrikaans translation of program ABC
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"
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
52 xliff
= self
.po2xliff(minipo
)
53 print "The generated xml:"
55 assert xliff
.translate("Applications") == "Toepassings"
56 assert xliff
.translate("bla") is None
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 "
70 xliff
= self
.po2xliff(minipo
)
71 print "The generated xml:"
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"
81 xliff
= self
.po2xliff(minipo
)
82 print "The generated xml:"
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"
97 xliff
= self
.po2xliff(minipo
)
98 print "The generated xml:"
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\""
114 msgstr "Gebruik \\\"."
116 xliff
= self
.po2xliff(minipo
)
117 print "The generated xml:"
120 assert xliff
.translate('Hello "Everyone"') == 'Good day "All"'
121 assert xliff
.translate(r
'Use \".') == r
'Gebruik \".'
122 assert xmltext
.find(r
'\"') > 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
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
136 xliff
= self
.po2xliff(minipo
)
137 print "The generated xml:"
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?
157 xliff
= self
.po2xliff(minipo
)
158 print "The generated xml:"
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.
181 xliff
= self
.po2xliff(minipo
)
182 print "The generated xml:"
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
202 "MIME-Version: 1.0\n"
204 xliff
= self
.po2xliff(minipo
)
205 print "The generated xml:"
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
224 xliff
= self
.po2xliff(minipo
)
225 print "The generated xml:"
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"
238 xliff
= self
.po2xliff(minipo
)
239 print "The generated xml:"
242 assert len(xliff
.units
) == 1
243 assert xliff
.translate("cow") == "inkomo"
245 def test_funny_plurals(self
):
246 minipo
= r
'''msgid "cow"
252 xliff
= self
.po2xliff(minipo
)
253 print "The generated xml:"
256 assert len(xliff
.units
) == 1
257 assert xliff
.translate("cow") == "inkomo"
259 def test_language_tags(self
):
260 minipo
= r
'''msgid "Een"
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
285 xliff
= self
.po2xliff(minipo
)
286 print "The generated xml:"
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()