fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_oo2po.py
blob419dd515071858c3f19ce5614528c5f499b4d0bf
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 from translate.convert import oo2po
5 from translate.convert import po2oo
6 from translate.convert import test_convert
7 from translate.misc import wStringIO
8 from translate.storage import po
9 from translate.storage.poheader import poheader
10 from translate.storage import oo
11 import os
13 class TestOO2PO:
14 target_filetype = po.pofile
15 conversion_module = oo2po
16 conversion_class = oo2po.oo2po
18 def convert(self, oosource, sourcelanguage='en-US', targetlanguage='af-ZA'):
19 """helper that converts oo source to po source without requiring files"""
20 inputoo = oo.oofile(oosource)
21 convertor = self.conversion_class(sourcelanguage, targetlanguage)
22 outputpo = convertor.convertstore(inputoo)
23 return outputpo
25 def singleelement(self, pofile):
26 """checks that the pofile contains a single non-header element, and returns it"""
27 if isinstance(pofile, poheader):
28 assert len(pofile.units) == 2
29 assert pofile.units[0].isheader()
30 return pofile.units[1]
31 else:
32 assert len(pofile.units) == 1
33 return pofile.units[0]
35 def roundtripstring(self, filename, entitystring):
36 """Convert the supplied string as part of an OpenOffice.org GSI file to po and back.
38 Return the string once it has been through all the conversions."""
40 ootemplate = r'helpcontent2 %s 0 help par_id3150670 35 0 en-US %s 2002-02-02 02:02:02'
42 oosource = ootemplate % (filename, entitystring)
43 ooinputfile = wStringIO.StringIO(oosource)
44 ootemplatefile = wStringIO.StringIO(oosource)
45 pooutputfile = wStringIO.StringIO()
47 self.conversion_module.convertoo(ooinputfile, pooutputfile, ootemplatefile, targetlanguage='en-US')
48 posource = pooutputfile.getvalue()
50 poinputfile = wStringIO.StringIO(posource)
51 ootemplatefile = wStringIO.StringIO(oosource)
52 oooutputfile = wStringIO.StringIO()
53 po2oo.convertoo(poinputfile, oooutputfile, ootemplatefile, targetlanguage="en-US")
54 ooresult = oooutputfile.getvalue()
55 print "original oo:\n", oosource, "po version:\n", posource, "output oo:\n", ooresult
56 return ooresult.split('\t')[10]
58 def check_roundtrip(self, filename, text):
59 """Checks that the text converted to po and back is the same as the original."""
60 assert self.roundtripstring(filename, text) == text
62 def test_simpleentity(self):
63 """checks that a simple oo entry converts properly to a po entry"""
64 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58'
65 pofile = self.convert(oosource)
66 pounit = self.singleelement(pofile)
67 assert pounit.source == "Character"
68 assert pounit.target == ""
70 def test_escapes(self):
71 """checks that a simple oo entry converts escapes properly to a po entry"""
72 oosource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 en-US Newline \n Newline Tab \t Tab CR \r CR 20050924 09:13:58"
73 pofile = self.convert(oosource)
74 pounit = self.singleelement(pofile)
75 poelementsrc = str(pounit)
76 print poelementsrc
77 assert "Newline \n Newline" in pounit.source
78 assert "Tab \t Tab" in pounit.source
79 assert "CR \r CR" in pounit.source
81 def test_roundtrip_escape(self):
82 self.check_roundtrip('strings.src', r'The given command is not a SELECT statement.\nOnly queries are allowed.')
83 self.check_roundtrip('source\ui\dlg\AutoControls_tmpl.hrc', r';\t59\t,\t44\t:\t58\t{Tab}\t9\t{Space}\t32')
84 self.check_roundtrip('inc_openoffice\windows\msi_languages\Nsis.ulf', r'The installation files must be unpacked and copied to your hard disk in preparation for the installation. After that, the %PRODUCTNAME installation will start automatically.\r\n\r\nClick \'Next\' to continue.')
85 self.check_roundtrip('file.xhp', r'\<asdf\>')
86 self.check_roundtrip('file.xhp', r'\<asdf prop=\"value\"\>')
87 self.check_roundtrip('file.xhp', r'\<asdf prop=\"value\"\>marked up text\</asdf\>')
88 self.check_roundtrip('file.xhp', r'\<asdf prop=\"value>>\"\>')
89 self.check_roundtrip('file.xhp', r'''\<asdf prop=\"value>>\"\>'Next'>> or "<<Previous"\</asdf\>''')
90 self.check_roundtrip('address_auto.xhp', r'''example, \<item type=\"literal\"\>'Harry\\'s Bar'.\</item\>''')
92 def xtest_roundtrip_whitespaceonly(self):
93 """check items that are only special instances of whitespce"""
94 # FIXME We can't roundtrip this yet because of some problems in the oo class handling
95 self.check_roundtrip('choose_chart_type.xhp', r' ')
96 self.check_roundtrip('choose_chart_type.xhp', '\xc2\xa0')
98 def test_double_escapes(self):
99 oosource = r"helpcontent2 source\text\shared\01\02100001.xhp 0 help par_id3150670 35 0 en-US \\< 2002-02-02 02:02:02"
100 pofile = self.convert(oosource)
101 pounit = self.singleelement(pofile)
102 poelementsrc = str(pounit)
103 print poelementsrc
104 assert pounit.source == r"\<"
106 def test_escapes_helpcontent2(self):
107 """checks that a helpcontent2 entry converts escapes properly to a po entry"""
108 oosource = r"helpcontent2 source\text\smath\guide\parentheses.xhp 0 help par_id3150344 4 0 en-US size *2 \\langle x \\rangle 2002-02-02 02:02:02"
109 pofile = self.convert(oosource)
110 pounit = self.singleelement(pofile)
111 poelementsrc = str(pounit)
112 print poelementsrc
113 assert pounit.source == r'size *2 \langle x \rangle'
115 def test_msgid_bug_error_address(self):
116 """tests the we have the correct url for reporting msgid bugs"""
117 oosource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 en-US Newline \n Newline Tab \t Tab CR \r CR 20050924 09:13:58"
118 bug_url = '''http://qa.openoffice.org/issues/enter_bug.cgi''' + ('''?subcomponent=ui&comment=&short_desc=Localization issue in file: &component=l10n&form_name=enter_issue''').replace(" ", "%20").replace(":", "%3A")
119 pofile = self.convert(oosource)
120 assert pofile.units[0].isheader()
121 assert pofile.parseheader()["Report-Msgid-Bugs-To"] == bug_url
123 def test_x_comment_inclusion(self):
124 """test that we can merge x-comment language entries into comment sections of the PO file"""
125 en_USsource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 en-US Text Quickhelp Title 20050924 09:13:58"
126 xcommentsource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 x-comment %s %s %s 20050924 09:13:58"
127 # Real comment
128 comment = "Comment"
129 commentsource = en_USsource + '\n' + xcommentsource % (comment, comment, comment)
130 pofile = self.convert(commentsource)
131 if isinstance(pofile, poheader):
132 units = pofile.units[1:]
133 else:
134 units = pofile.units
135 textunit = units[0]
136 assert textunit.source == "Text"
137 assert comment in textunit.getnotes("developer")
138 quickhelpunit = units[1]
139 assert quickhelpunit.source == "Quickhelp"
140 assert comment in quickhelpunit.getnotes("developer")
141 titleunit = units[2]
142 assert titleunit.source == "Title"
143 assert comment in titleunit.getnotes("developer")
144 # Whitespace and blank
145 for comment in (" ", ""):
146 commentsource = en_USsource + '\n' + xcommentsource % (comment, comment, comment)
147 pofile = self.convert(commentsource)
148 if isinstance(pofile, poheader):
149 units = pofile.units[1:]
150 else:
151 units = pofile.units
152 textunit = units[0]
153 assert textunit.source == "Text"
154 assert textunit.getnotes("developer") == ""
155 quickhelpunit = units[1]
156 assert quickhelpunit.source == "Quickhelp"
157 assert quickhelpunit.getnotes("developer") == ""
158 titleunit = units[2]
159 assert titleunit.source == "Title"
160 assert titleunit.getnotes("developer") == ""
162 class TestOO2POCommand(test_convert.TestConvertCommand, TestOO2PO):
163 """Tests running actual oo2po commands on files"""
164 convertmodule = oo2po
166 def test_help(self):
167 """tests getting help"""
168 options = test_convert.TestConvertCommand.test_help(self)
169 options = self.help_check(options, "--source-language=LANG")
170 options = self.help_check(options, "--language=LANG")
171 options = self.help_check(options, "-P, --pot")
172 options = self.help_check(options, "--duplicates=DUPLICATESTYLE")
173 options = self.help_check(options, "--multifile=MULTIFILESTYLE")
174 options = self.help_check(options, "--nonrecursiveinput", last=True)
176 def test_preserve_filename(self):
177 """Ensures that the filename is preserved."""
178 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58'
179 self.create_testfile("snippet.sdf", oosource)
180 oofile = oo.oofile(self.open_testfile("snippet.sdf"))
181 assert oofile.filename.endswith("snippet.sdf")
182 oofile.parse(oosource)
183 assert oofile.filename.endswith("snippet.sdf")
185 def test_simple_pot(self):
186 """tests the simplest possible conversion to a pot file"""
187 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58'
188 self.create_testfile("simple.oo", oosource)
189 self.run_command("simple.oo", "simple.pot", pot=True, nonrecursiveinput=True)
190 pofile = self.target_filetype(self.open_testfile("simple.pot"))
191 poelement = self.singleelement(pofile)
192 assert poelement.source == "Character"
193 assert poelement.target == ""
195 def test_simple_po(self):
196 """tests the simplest possible conversion to a po file"""
197 oosource1 = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58'
198 oosource2 = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 ku Karakter 20050924 09:13:58'
199 self.create_testfile("simple.oo", oosource1 + "\n" + oosource2)
200 self.run_command("simple.oo", "simple.po", lang="ku", nonrecursiveinput=True)
201 pofile = self.target_filetype(self.open_testfile("simple.po"))
202 poelement = self.singleelement(pofile)
203 assert poelement.source == "Character"
204 assert poelement.target == "Karakter"
206 def test_onefile_nonrecursive(self):
207 """tests the --multifile=onefile option and make sure it doesn't produce a directory"""
208 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58'
209 self.create_testfile("simple.oo", oosource)
210 self.run_command("simple.oo", "simple.pot", pot=True, multifile="onefile")
211 assert os.path.isfile(self.get_testfilename("simple.pot"))
213 def test_remove_duplicates(self):
214 """test that removing of duplicates works correctly (bug 171)"""
215 oosource = r'''
216 sd source\ui\animations\SlideTransitionPane.src 0 checkbox DLG_SLIDE_TRANSITION_PANE CB_AUTO_PREVIEW HID_SD_SLIDETRANSITIONPANE_CB_AUTO_PREVIEW 1 en-US Automatic preview 20060725 03:26:42
217 sd source\ui\animations\AnimationSchemesPane.src 0 checkbox DLG_ANIMATION_SCHEMES_PANE CB_AUTO_PREVIEW HID_SD_ANIMATIONSCHEMESPANE_CB_AUTO_PREVIEW 1 en-US Automatic preview 20060725 03:26:42
218 sd source\ui\animations\CustomAnimationCreateDialog.src 0 checkbox RID_TP_CUSTOMANIMATION_ENTRANCE CBX_PREVIEW 143 en-US Automatic preview 20060725 03:26:42
219 sd source\ui\animations\CustomAnimationCreateDialog.src 0 checkbox RID_TP_CUSTOMANIMATION_ENTRANCE CBX_PREVIEW 143 fr Aperçu automatique 20060725 03:26:42
220 sd source\ui\animations\CustomAnimationSchemesPane.src 0 checkbox DLG_CUSTOMANIMATION_SCHEMES_PANE 4 0 en-US Automatic preview 20060725 03:26:42
221 sd source\ui\animations\CustomAnimationSchemesPane.src 0 checkbox DLG_CUSTOMANIMATION_SCHEMES_PANE 4 0 fr Aperçu automatique 20060725 03:26:42
223 self.create_testfile("simple.oo", oosource)
224 self.run_command("simple.oo", "simple.po", language="fr", multifile="onefile", error="traceback", duplicates="merge")
225 pofile = self.target_filetype(self.open_testfile("simple.po"))
226 assert len(pofile.units) == 2
227 assert pofile.units[1].target == u"Aperçu automatique"