fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_xliff2po.py
blob1ea78f8bd5491a2faaa6b4f1f74c2aa8f8849907
1 #!/usr/bin/env python
3 from translate.convert import xliff2po
4 from translate.misc import wStringIO
6 class TestXLIFF2PO:
7 xliffskeleton = '''<?xml version="1.0" ?>
8 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
9 <file original="filename.po" source-language="en-US" datatype="po">
10 <body>
12 </body>
13 </file>
14 </xliff>'''
16 def xliff2po(self, xliffsource):
17 """helper that converts xliff source to po source without requiring files"""
18 inputfile = wStringIO.StringIO(xliffsource)
19 convertor = xliff2po.xliff2po()
20 outputpo = convertor.convertstore(inputfile)
21 print "The generated po:"
22 print type(outputpo)
23 print str(outputpo)
24 return outputpo
26 def test_minimal(self):
27 minixlf = self.xliffskeleton % '''<trans-unit>
28 <source>red</source>
29 <target>rooi</target>
30 </trans-unit>'''
31 pofile = self.xliff2po(minixlf)
32 assert len(pofile.units) == 1
33 assert pofile.translate("red") == "rooi"
34 assert pofile.translate("bla") is None
36 def test_basic(self):
37 headertext = '''Project-Id-Version: program 2.1-branch
38 Report-Msgid-Bugs-To:
39 POT-Creation-Date: 2006-01-09 07:15+0100
40 PO-Revision-Date: 2004-03-30 17:02+0200
41 Last-Translator: Zuza Software Foundation &lt;xxx@translate.org.za>
42 Language-Team: Afrikaans &lt;translate-discuss-xxx@lists.sourceforge.net>
43 MIME-Version: 1.0
44 Content-Type: text/plain; charset=UTF-8
45 Content-Transfer-Encoding: 8bit'''
47 minixlf = (self.xliffskeleton % '''<trans-unit id="1" restype="x-gettext-domain-header" approved="no" xml:space="preserve">
48 <source>%s</source>
49 <target>%s</target>
50 <note from="po-translator">Zulu translation of program ABC</note>
51 </trans-unit>
52 <trans-unit>
53 <source>gras</source>
54 <target>utshani</target>
55 </trans-unit>''') % (headertext, headertext)
57 print minixlf
58 pofile = self.xliff2po(minixlf)
59 assert pofile.translate("gras") == "utshani"
60 assert pofile.translate("bla") is None
61 potext = str(pofile)
62 assert potext.index('# Zulu translation of program ABC') == 0
63 assert potext.index('msgid "gras"\n')
64 assert potext.index('msgstr "utshani"\n')
65 assert potext.index('MIME-Version: 1.0\\n')
67 def test_translatorcomments(self):
68 """Tests translator comments"""
69 minixlf = self.xliffskeleton % '''<trans-unit>
70 <source>nonsense</source>
71 <target>matlhapolosa</target>
72 <context-group name="po-entry" purpose="information">
73 <context context-type="x-po-trancomment">Couldn't do
74 it</context>
75 </context-group>
76 <note from="po-translator">Couldn't do
77 it</note>
78 </trans-unit>'''
79 pofile = self.xliff2po(minixlf)
80 assert pofile.translate("nonsense") == "matlhapolosa"
81 assert pofile.translate("bla") is None
82 unit = pofile.units[0]
83 assert unit.getnotes("translator") == "Couldn't do\nit"
84 potext = str(pofile)
85 assert potext.index("# Couldn't do\n# it\n") >= 0
87 def test_autocomment(self):
88 """Tests automatic comments"""
89 minixlf = self.xliffskeleton % '''<trans-unit>
90 <source>nonsense</source>
91 <target>matlhapolosa</target>
92 <context-group name="po-entry" purpose="information">
93 <context context-type="x-po-autocomment">Note that this is
94 garbage</context>
95 </context-group>
96 <note from="developer">Note that this is
97 garbage</note>
98 </trans-unit>'''
99 pofile = self.xliff2po(minixlf)
100 assert pofile.translate("nonsense") == "matlhapolosa"
101 assert pofile.translate("bla") is None
102 unit = pofile.units[0]
103 assert unit.getnotes("developer") == "Note that this is\ngarbage"
104 potext = str(pofile)
105 assert potext.index("#. Note that this is\n#. garbage\n") >= 0
107 def test_locations(self):
108 """Tests location comments (#:)"""
109 minixlf = self.xliffskeleton % '''<trans-unit id="1">
110 <source>nonsense</source>
111 <target>matlhapolosa</target>
112 <context-group name="po-reference" purpose="location">
113 <context context-type="sourcefile">example.c</context>
114 <context context-type="linenumber">123</context>
115 </context-group>
116 <context-group name="po-reference" purpose="location">
117 <context context-type="sourcefile">place.py</context>
118 </context-group>
119 </trans-unit>'''
120 pofile = self.xliff2po(minixlf)
121 assert pofile.translate("nonsense") == "matlhapolosa"
122 assert pofile.translate("bla") is None
123 unit = pofile.units[0]
124 locations = unit.getlocations()
125 assert len(locations) == 2
126 assert "example.c:123" in locations
127 assert "place.py" in locations
129 def test_fuzzy(self):
130 """Tests fuzzyness"""
131 minixlf = self.xliffskeleton % '''<trans-unit approved="no">
132 <source>book</source>
133 </trans-unit>
134 <trans-unit id="2" approved="yes">
135 <source>nonsense</source>
136 <target>matlhapolosa</target>
137 </trans-unit>
138 <trans-unit id="2" approved="no">
139 <source>verb</source>
140 <target state="needs-review-translation">lediri</target>
141 </trans-unit>'''
142 pofile = self.xliff2po(minixlf)
143 assert pofile.translate("nonsense") == "matlhapolosa"
144 assert pofile.translate("verb") == "lediri"
145 assert pofile.translate("book") is None
146 assert pofile.translate("bla") is None
147 assert len(pofile.units) == 3
148 #TODO: decide if this one should be fuzzy:
149 #assert pofile.units[0].isfuzzy()
150 assert not pofile.units[1].isfuzzy()
151 assert pofile.units[2].isfuzzy()
153 def test_plurals(self):
154 """Tests fuzzyness"""
155 minixlf = self.xliffskeleton % '''<group restype="x-gettext-plurals">
156 <trans-unit id="1[0]" xml:space="preserve">
157 <source>cow</source>
158 <target>inkomo</target>
159 </trans-unit>
160 <trans-unit id="1[1]" xml:space="preserve">
161 <source>cows</source>
162 <target>iinkomo</target>
163 </trans-unit>
164 </group>'''
165 pofile = self.xliff2po(minixlf)
166 print str(pofile)
167 potext = str(pofile)
168 assert len(pofile.units) == 1
169 assert potext.index('msgid_plural "cows"')
170 assert potext.index('msgstr[0] "inkomo"')
171 assert potext.index('msgstr[1] "iinkomo"')
174 class TestBasicXLIFF2PO(TestXLIFF2PO):
175 """This tests a basic XLIFF file without xmlns attribute"""
177 xliffskeleton = '''<?xml version="1.0" ?>
178 <xliff version="1.1">
179 <file original="filename.po" source-language="en-US" datatype="po">
180 <body>
182 </body>
183 </file>
184 </xliff>'''