3 from translate
.convert
import xliff2po
4 from translate
.misc
import wStringIO
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">
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:"
26 def test_minimal(self
):
27 minixlf
= self
.xliffskeleton
% '''<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
37 headertext
= '''Project-Id-Version: program 2.1-branch
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 <xxx@translate.org.za>
42 Language-Team: Afrikaans <translate-discuss-xxx@lists.sourceforge.net>
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">
50 <note from="po-translator">Zulu translation of program ABC</note>
54 <target>utshani</target>
55 </trans-unit>''') % (headertext
, headertext
)
58 pofile
= self
.xliff2po(minixlf
)
59 assert pofile
.translate("gras") == "utshani"
60 assert pofile
.translate("bla") is None
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
76 <note from="po-translator">Couldn't do
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"
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
96 <note from="developer">Note that this is
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"
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>
116 <context-group name="po-reference" purpose="location">
117 <context context-type="sourcefile">place.py</context>
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>
134 <trans-unit id="2" approved="yes">
135 <source>nonsense</source>
136 <target>matlhapolosa</target>
138 <trans-unit id="2" approved="no">
139 <source>verb</source>
140 <target state="needs-review-translation">lediri</target>
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">
158 <target>inkomo</target>
160 <trans-unit id="1[1]" xml:space="preserve">
161 <source>cows</source>
162 <target>iinkomo</target>
165 pofile
= self
.xliff2po(minixlf
)
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">