1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
import os
import base64
import gettext
import unittest
from unittest import TestCase
# TODO:
# - Add new tests, for example for "dgettext"
# - Remove dummy tests, for example testing for single and double quotes
# has no sense, it would have if we were testing a parser (i.e. pygettext)
# - Tests should have only one assert.
GNU_MO_DATA = '''\
3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAF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AYmFjb24Ad2luayB3aW5rAA==
'''
LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
def setup():
os.makedirs(LOCALEDIR)
fp = open(MOFILE, 'wb')
fp.write(base64.decodestring(GNU_MO_DATA))
fp.close()
os.environ['LANGUAGE'] = 'xx'
def teardown():
os.environ['LANGUAGE'] = 'en'
os.unlink(MOFILE)
os.removedirs(LOCALEDIR)
class GettextTestCase1(TestCase):
def setUp(self):
self.localedir = os.curdir
self.mofile = MOFILE
gettext.install('gettext', self.localedir)
def test_some_translations(self):
# test some translations
assert _('albatross') == 'albatross'
assert _(u'mullusk') == 'bacon'
assert _(r'Raymond Luxury Yach-t') == 'Throatwobbler Mangrove'
assert _(ur'nudge nudge') == 'wink wink'
def test_double_quotes(self):
# double quotes
assert _("albatross") == 'albatross'
assert _(u"mullusk") == 'bacon'
assert _(r"Raymond Luxury Yach-t") == 'Throatwobbler Mangrove'
assert _(ur"nudge nudge") == 'wink wink'
def test_triple_single_quotes(self):
# triple single quotes
assert _('''albatross''') == 'albatross'
assert _(u'''mullusk''') == 'bacon'
assert _(r'''Raymond Luxury Yach-t''') == 'Throatwobbler Mangrove'
assert _(ur'''nudge nudge''') == 'wink wink'
def test_triple_double_quotes(self):
# triple double quotes
assert _("""albatross""") == 'albatross'
assert _(u"""mullusk""") == 'bacon'
assert _(r"""Raymond Luxury Yach-t""") == 'Throatwobbler Mangrove'
assert _(ur"""nudge nudge""") == 'wink wink'
def test_multiline_strings(self):
# multiline strings
assert _('''This module provides internationalization and localization
support for your Python programs by providing an interface to the GNU
gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
trggrkg zrffntr pngnybt yvoenel.'''
def test_the_alternative_interface(self):
# test the alternative interface
fp = open(os.path.join(self.mofile), 'rb')
t = gettext.GNUTranslations(fp)
fp.close()
t.install()
assert _('nudge nudge') == 'wink wink'
# try unicode return type
t.install(unicode=1)
assert _('mullusk') == 'bacon'
class GettextTestCase2(TestCase):
def setUp(self):
self.localedir = os.curdir
gettext.bindtextdomain('gettext', self.localedir)
gettext.textdomain('gettext')
self._ = gettext.gettext
def test_bindtextdomain(self):
assert gettext.bindtextdomain('gettext') == self.localedir
def test_textdomain(self):
assert gettext.textdomain() == 'gettext'
def test_some_translations(self):
# test some translations
assert self._('albatross') == 'albatross'
assert self._(u'mullusk') == 'bacon'
assert self._(r'Raymond Luxury Yach-t') == 'Throatwobbler Mangrove'
assert self._(ur'nudge nudge') == 'wink wink'
def test_double_quotes(self):
# double quotes
assert self._("albatross") == 'albatross'
assert self._(u"mullusk") == 'bacon'
assert self._(r"Raymond Luxury Yach-t") == 'Throatwobbler Mangrove'
assert self._(ur"nudge nudge") == 'wink wink'
def test_triple_single_quotes(self):
# triple single quotes
assert self._('''albatross''') == 'albatross'
assert self._(u'''mullusk''') == 'bacon'
assert self._(r'''Raymond Luxury Yach-t''') == 'Throatwobbler Mangrove'
assert self._(ur'''nudge nudge''') == 'wink wink'
def test_triple_double_quotes(self):
# triple double quotes
assert self._("""albatross""") == 'albatross'
assert self._(u"""mullusk""") == 'bacon'
assert self._(r"""Raymond Luxury Yach-t""") == 'Throatwobbler Mangrove'
assert self._(ur"""nudge nudge""") == 'wink wink'
def test_multiline_strings(self):
# multiline strings
assert self._('''This module provides internationalization and localization
support for your Python programs by providing an interface to the GNU
gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
trggrkg zrffntr pngnybt yvoenel.'''
class PluralFormsTestCase(TestCase):
def setUp(self):
self.mofile = MOFILE
def test_plural_forms1(self):
x = gettext.ngettext('There is %s file', 'There are %s files', 1)
assert x == 'Hay %s fichero'
x = gettext.ngettext('There is %s file', 'There are %s files', 2)
assert x == 'Hay %s ficheros'
def test_plural_forms2(self):
fp = open(os.path.join(self.mofile), 'rb')
t = gettext.GNUTranslations(fp)
fp.close()
x = t.ngettext('There is %s file', 'There are %s files', 1)
assert x == 'Hay %s fichero'
x = t.ngettext('There is %s file', 'There are %s files', 2)
assert x == 'Hay %s ficheros'
def test_hu(self):
f = gettext.c2py('0')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
def test_de(self):
f = gettext.c2py('n != 1')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
def test_fr(self):
f = gettext.c2py('n>1')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
def test_gd(self):
f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"
def test_gd2(self):
# Tests the combination of parentheses and "?:"
f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"
def test_lt(self):
f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111"
def test_ru(self):
f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222"
def test_pl(self):
f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222"
def test_sl(self):
f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
s = ''.join([ str(f(x)) for x in range(200) ])
assert s == "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"
def test_security(self):
# Test for a dangerous expression
try:
gettext.c2py("os.chmod('/etc/passwd',0777)")
except ValueError:
pass
else:
raise AssertionError
if __name__ == '__main__':
try:
setup()
unittest.main()
finally:
teardown()
# For reference, here's the .po file used to created the .mo data above.
#
# The original version was automatically generated from the sources with
# pygettext. Later it was manually modified to add plural forms support.
'''
# Dummy translation for Python's test_gettext.py module.
# Copyright (C) 2001 Python Software Foundation
# Barry Warsaw <barry@python.org>, 2000.
#
msgid ""
msgstr ""
"Project-Id-Version: 2.0\n"
"PO-Revision-Date: 2000-08-29 12:19-04:00\n"
"Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
"Language-Team: XX <python-dev@python.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.1\n"
"Plural-Forms: nplurals=2; plural=n!=1;\n"
#: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
#: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
#: test_gettext.py:98
msgid "nudge nudge"
msgstr "wink wink"
#: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
#: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
msgid "albatross"
msgstr ""
#: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
#: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
msgid "Raymond Luxury Yach-t"
msgstr "Throatwobbler Mangrove"
#: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
#: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
#: test_gettext.py:96
msgid "mullusk"
msgstr "bacon"
#: test_gettext.py:40 test_gettext.py:101
msgid ""
"This module provides internationalization and localization\n"
"support for your Python programs by providing an interface to the GNU\n"
"gettext message catalog library."
msgstr ""
"Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
"fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
"trggrkg zrffntr pngnybt yvoenel."
# Manually added, as neither pygettext nor xgettext support plural forms
# in Python.
msgid "There is %s file"
msgid_plural "There are %s files"
msgstr[0] "Hay %s fichero"
msgstr[1] "Hay %s ficheros"
'''
|