blob: a875272bd3e28d26bac60eda7b7941cfd39228ac [file] [log] [blame]
Barry Warsaw9e035922000-08-25 19:14:25 +00001import os
Barry Warsawff6d8132000-08-25 19:50:38 +00002import base64
Barry Warsawa1ce93f2003-04-11 18:36:43 +00003import shutil
Barry Warsaw9e035922000-08-25 19:14:25 +00004import gettext
Martin v. Löwisd8996052002-11-21 21:45:32 +00005import unittest
Barry Warsawa1ce93f2003-04-11 18:36:43 +00006
Guido van Rossumd8faa362007-04-27 19:54:29 +00007from test import test_support
Barry Warsawa1ce93f2003-04-11 18:36:43 +00008
Fred Drake004d5e62000-10-23 17:22:08 +00009
Martin v. Löwisd8996052002-11-21 21:45:32 +000010# TODO:
11# - Add new tests, for example for "dgettext"
12# - Remove dummy tests, for example testing for single and double quotes
13# has no sense, it would have if we were testing a parser (i.e. pygettext)
14# - Tests should have only one assert.
Barry Warsaw64dab462000-08-30 03:32:07 +000015
Guido van Rossum39478e82007-08-27 17:23:59 +000016GNU_MO_DATA = b'''\
Martin v. Löwisd8996052002-11-21 21:45:32 +0000173hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
18AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
19AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
20eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
21aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
22CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
23Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
24ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
25MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
26YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN
27SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4
28NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0
29ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0
30d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
31eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
32IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
33ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
Barry Warsawff6d8132000-08-25 19:50:38 +000034'''
35
Guido van Rossum39478e82007-08-27 17:23:59 +000036UMO_DATA = b'''\
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000373hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABAAAAFEAAAAPAQAAVgAAAAQAAABm
38AQAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAYWLDngBQcm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1S
39ZXZpc2lvbi1EYXRlOiAyMDAzLTA0LTExIDEyOjQyLTA0MDAKTGFzdC1UcmFuc2xhdG9yOiBCYXJy
40eSBBLiBXQXJzYXcgPGJhcnJ5QHB5dGhvbi5vcmc+Ckxhbmd1YWdlLVRlYW06IFhYIDxweXRob24t
41ZGV2QHB5dGhvbi5vcmc+Ck1JTUUtVmVyc2lvbjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFp
42bjsgY2hhcnNldD11dGYtOApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0CkdlbmVyYXRl
43ZC1CeTogbWFudWFsbHkKAMKkeXoA
44'''
Fred Drake004d5e62000-10-23 17:22:08 +000045
Guido van Rossum39478e82007-08-27 17:23:59 +000046MMO_DATA = b'''\
Barry Warsawe960e222003-05-20 17:28:54 +0000473hIElQAAAAABAAAAHAAAACQAAAADAAAALAAAAAAAAAA4AAAAeAEAADkAAAABAAAAAAAAAAAAAAAA
48UHJvamVjdC1JZC1WZXJzaW9uOiBObyBQcm9qZWN0IDAuMApQT1QtQ3JlYXRpb24tRGF0ZTogV2Vk
49IERlYyAxMSAwNzo0NDoxNSAyMDAyClBPLVJldmlzaW9uLURhdGU6IDIwMDItMDgtMTQgMDE6MTg6
50NTgrMDA6MDAKTGFzdC1UcmFuc2xhdG9yOiBKb2huIERvZSA8amRvZUBleGFtcGxlLmNvbT4KSmFu
51ZSBGb29iYXIgPGpmb29iYXJAZXhhbXBsZS5jb20+Ckxhbmd1YWdlLVRlYW06IHh4IDx4eEBleGFt
52cGxlLmNvbT4KTUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFy
53c2V0PWlzby04ODU5LTE1CkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFi
54bGUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4zCgA=
55'''
56
Barry Warsawff6d8132000-08-25 19:50:38 +000057LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
58MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
Barry Warsawa1ce93f2003-04-11 18:36:43 +000059UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
Barry Warsawe960e222003-05-20 17:28:54 +000060MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
Brett Cannon4aebbb02003-04-27 19:42:41 +000061try:
62 LANG = os.environ['LANGUAGE']
63except:
64 LANG = 'en'
Barry Warsawff6d8132000-08-25 19:50:38 +000065
66
Barry Warsawa1ce93f2003-04-11 18:36:43 +000067class GettextBaseTest(unittest.TestCase):
Martin v. Löwisd8996052002-11-21 21:45:32 +000068 def setUp(self):
Neal Norwitza0e0cd32004-07-19 00:08:59 +000069 if not os.path.isdir(LOCALEDIR):
70 os.makedirs(LOCALEDIR)
Barry Warsawa1ce93f2003-04-11 18:36:43 +000071 fp = open(MOFILE, 'wb')
72 fp.write(base64.decodestring(GNU_MO_DATA))
73 fp.close()
74 fp = open(UMOFILE, 'wb')
75 fp.write(base64.decodestring(UMO_DATA))
76 fp.close()
Barry Warsawe960e222003-05-20 17:28:54 +000077 fp = open(MMOFILE, 'wb')
78 fp.write(base64.decodestring(MMO_DATA))
79 fp.close()
Barry Warsawa1ce93f2003-04-11 18:36:43 +000080 os.environ['LANGUAGE'] = 'xx'
81
82 def tearDown(self):
Brett Cannon4aebbb02003-04-27 19:42:41 +000083 os.environ['LANGUAGE'] = LANG
84 shutil.rmtree(os.path.split(LOCALEDIR)[0])
Barry Warsawa1ce93f2003-04-11 18:36:43 +000085
86
87class GettextTestCase1(GettextBaseTest):
88 def setUp(self):
89 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +000090 self.localedir = os.curdir
91 self.mofile = MOFILE
Martin v. Löwisd8996052002-11-21 21:45:32 +000092 gettext.install('gettext', self.localedir)
93
Martin v. Löwisd8996052002-11-21 21:45:32 +000094 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +000095 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +000096 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +000097 eq(_('albatross'), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +000098 eq(_('mullusk'), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +000099 eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000100 eq(_(r'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000101
102 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000103 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000104 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000105 eq(_("albatross"), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000106 eq(_("mullusk"), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000107 eq(_(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000108 eq(_(r"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000109
110 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000111 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000112 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000113 eq(_('''albatross'''), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000114 eq(_('''mullusk'''), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000115 eq(_(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000116 eq(_(r'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000117
118 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000119 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000120 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000121 eq(_("""albatross"""), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000122 eq(_("""mullusk"""), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000123 eq(_(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000124 eq(_(r"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000125
126 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000127 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000128 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000129 eq(_('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000130support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000131gettext message catalog library.'''),
132 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000133fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000134trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000135
136 def test_the_alternative_interface(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000137 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000138 # test the alternative interface
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000139 fp = open(self.mofile, 'rb')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000140 t = gettext.GNUTranslations(fp)
141 fp.close()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000142 # Install the translation object
Martin v. Löwisd8996052002-11-21 21:45:32 +0000143 t.install()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000144 eq(_('nudge nudge'), 'wink wink')
145 # Try unicode return type
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000146 t.install(str=True)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000147 eq(_('mullusk'), 'bacon')
Georg Brandl602b9ba2006-02-19 13:26:36 +0000148 # Test installation of other methods
Georg Brandl1a3284e2007-12-02 09:40:06 +0000149 import builtins
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000150 t.install(str=True, names=["gettext", "lgettext"])
Georg Brandl602b9ba2006-02-19 13:26:36 +0000151 eq(_, t.ugettext)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000152 eq(builtins.gettext, t.ugettext)
Georg Brandl602b9ba2006-02-19 13:26:36 +0000153 eq(lgettext, t.lgettext)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000154 del builtins.gettext
155 del builtins.lgettext
Martin v. Löwisd8996052002-11-21 21:45:32 +0000156
157
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000158class GettextTestCase2(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000159 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000160 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000161 self.localedir = os.curdir
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000162 # Set up the bindings
Martin v. Löwisd8996052002-11-21 21:45:32 +0000163 gettext.bindtextdomain('gettext', self.localedir)
164 gettext.textdomain('gettext')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000165 # For convenience
Martin v. Löwisd8996052002-11-21 21:45:32 +0000166 self._ = gettext.gettext
167
Martin v. Löwisd8996052002-11-21 21:45:32 +0000168 def test_bindtextdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000169 self.assertEqual(gettext.bindtextdomain('gettext'), self.localedir)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000170
171 def test_textdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000172 self.assertEqual(gettext.textdomain(), 'gettext')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000173
174 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000175 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000176 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000177 eq(self._('albatross'), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000178 eq(self._('mullusk'), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000179 eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000180 eq(self._(r'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000181
182 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000183 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000184 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000185 eq(self._("albatross"), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000186 eq(self._("mullusk"), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000187 eq(self._(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000188 eq(self._(r"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000189
190 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000191 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000192 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000193 eq(self._('''albatross'''), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000194 eq(self._('''mullusk'''), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000195 eq(self._(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000196 eq(self._(r'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000197
198 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000199 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000200 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000201 eq(self._("""albatross"""), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000202 eq(self._("""mullusk"""), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000203 eq(self._(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000204 eq(self._(r"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000205
206 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000207 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000208 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000209 eq(self._('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000210support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000211gettext message catalog library.'''),
212 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000213fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000214trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000215
216
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000217class PluralFormsTestCase(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000218 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000219 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000220 self.mofile = MOFILE
221
222 def test_plural_forms1(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000223 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000224 x = gettext.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000225 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000226 x = gettext.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000227 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000228
229 def test_plural_forms2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000230 eq = self.assertEqual
231 fp = open(self.mofile, 'rb')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000232 t = gettext.GNUTranslations(fp)
233 fp.close()
Martin v. Löwisd8996052002-11-21 21:45:32 +0000234 x = t.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000235 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000236 x = t.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000237 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000238
239 def test_hu(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000240 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000241 f = gettext.c2py('0')
242 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000243 eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000244
245 def test_de(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000246 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000247 f = gettext.c2py('n != 1')
248 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000249 eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000250
251 def test_fr(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000252 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000253 f = gettext.c2py('n>1')
254 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000255 eq(s, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000256
257 def test_gd(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000258 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000259 f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2')
260 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000261 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000262
263 def test_gd2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000264 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000265 # Tests the combination of parentheses and "?:"
266 f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
267 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000268 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000269
270 def test_lt(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000271 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000272 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
273 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000274 eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000275
276 def test_ru(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000277 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000278 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
279 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000280 eq(s, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000281
282 def test_pl(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000283 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000284 f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
285 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000286 eq(s, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000287
288 def test_sl(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000289 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000290 f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
291 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000292 eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000293
294 def test_security(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000295 raises = self.assertRaises
Martin v. Löwisd8996052002-11-21 21:45:32 +0000296 # Test for a dangerous expression
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000297 raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000298
299
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000300class UnicodeTranslationsTest(GettextBaseTest):
301 def setUp(self):
302 GettextBaseTest.setUp(self)
303 fp = open(UMOFILE, 'rb')
304 try:
Barry Warsawedb155f2003-04-24 18:08:13 +0000305 self.t = gettext.GNUTranslations(fp)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000306 finally:
307 fp.close()
308 self._ = self.t.ugettext
309
310 def test_unicode_msgid(self):
311 unless = self.failUnless
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000312 unless(isinstance(self._(''), str))
313 unless(isinstance(self._(''), str))
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000314
315 def test_unicode_msgstr(self):
316 eq = self.assertEqual
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000317 eq(self._('ab\xde'), '\xa4yz')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000318
319
Barry Warsawe960e222003-05-20 17:28:54 +0000320class WeirdMetadataTest(GettextBaseTest):
321 def setUp(self):
322 GettextBaseTest.setUp(self)
323 fp = open(MMOFILE, 'rb')
324 try:
325 try:
326 self.t = gettext.GNUTranslations(fp)
327 except:
328 self.tearDown()
329 raise
330 finally:
331 fp.close()
332
333 def test_weird_metadata(self):
334 info = self.t.info()
Christian Heimes6ae5d7f2007-10-31 18:53:44 +0000335 self.assertEqual(len(info), 9)
Barry Warsawe960e222003-05-20 17:28:54 +0000336 self.assertEqual(info['last-translator'],
337 'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
338
339
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000340def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000341 test_support.run_unittest(__name__)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000342
343if __name__ == '__main__':
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000344 test_main()
Martin v. Löwisd8996052002-11-21 21:45:32 +0000345
Barry Warsawff6d8132000-08-25 19:50:38 +0000346
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000347# For reference, here's the .po file used to created the GNU_MO_DATA above.
Martin v. Löwisd8996052002-11-21 21:45:32 +0000348#
349# The original version was automatically generated from the sources with
350# pygettext. Later it was manually modified to add plural forms support.
Barry Warsawff6d8132000-08-25 19:50:38 +0000351
Barry Warsaw64dab462000-08-30 03:32:07 +0000352'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000353# Dummy translation for the Python test_gettext.py module.
Barry Warsaw19148732001-08-24 19:11:57 +0000354# Copyright (C) 2001 Python Software Foundation
355# Barry Warsaw <barry@python.org>, 2000.
Barry Warsaw64dab462000-08-30 03:32:07 +0000356#
357msgid ""
358msgstr ""
359"Project-Id-Version: 2.0\n"
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000360"PO-Revision-Date: 2003-04-11 14:32-0400\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000361"Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000362"Language-Team: XX <python-dev@python.org>\n"
363"MIME-Version: 1.0\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000364"Content-Type: text/plain; charset=iso-8859-1\n"
365"Content-Transfer-Encoding: 8bit\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000366"Generated-By: pygettext.py 1.1\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000367"Plural-Forms: nplurals=2; plural=n!=1;\n"
Barry Warsawff6d8132000-08-25 19:50:38 +0000368
Barry Warsaw64dab462000-08-30 03:32:07 +0000369#: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
370#: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
371#: test_gettext.py:98
372msgid "nudge nudge"
373msgstr "wink wink"
Barry Warsawff6d8132000-08-25 19:50:38 +0000374
Barry Warsaw64dab462000-08-30 03:32:07 +0000375#: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
376#: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
377msgid "albatross"
378msgstr ""
Barry Warsawff6d8132000-08-25 19:50:38 +0000379
Barry Warsaw64dab462000-08-30 03:32:07 +0000380#: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
381#: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
382msgid "Raymond Luxury Yach-t"
383msgstr "Throatwobbler Mangrove"
Barry Warsawff6d8132000-08-25 19:50:38 +0000384
Barry Warsaw64dab462000-08-30 03:32:07 +0000385#: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
386#: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
387#: test_gettext.py:96
388msgid "mullusk"
389msgstr "bacon"
Barry Warsawff6d8132000-08-25 19:50:38 +0000390
Barry Warsaw64dab462000-08-30 03:32:07 +0000391#: test_gettext.py:40 test_gettext.py:101
392msgid ""
393"This module provides internationalization and localization\n"
394"support for your Python programs by providing an interface to the GNU\n"
395"gettext message catalog library."
396msgstr ""
397"Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
398"fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
399"trggrkg zrffntr pngnybt yvoenel."
Martin v. Löwisd8996052002-11-21 21:45:32 +0000400
401# Manually added, as neither pygettext nor xgettext support plural forms
402# in Python.
403msgid "There is %s file"
404msgid_plural "There are %s files"
405msgstr[0] "Hay %s fichero"
406msgstr[1] "Hay %s ficheros"
Barry Warsaw64dab462000-08-30 03:32:07 +0000407'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000408
409# Here's the second example po file example, used to generate the UMO_DATA
410# containing utf-8 encoded Unicode strings
411
412'''
413# Dummy translation for the Python test_gettext.py module.
414# Copyright (C) 2001 Python Software Foundation
415# Barry Warsaw <barry@python.org>, 2000.
416#
417msgid ""
418msgstr ""
419"Project-Id-Version: 2.0\n"
420"PO-Revision-Date: 2003-04-11 12:42-0400\n"
421"Last-Translator: Barry A. WArsaw <barry@python.org>\n"
422"Language-Team: XX <python-dev@python.org>\n"
423"MIME-Version: 1.0\n"
424"Content-Type: text/plain; charset=utf-8\n"
425"Content-Transfer-Encoding: 7bit\n"
426"Generated-By: manually\n"
427
428#: nofile:0
429msgid "ab\xc3\x9e"
430msgstr "\xc2\xa4yz"
431'''
Barry Warsawe960e222003-05-20 17:28:54 +0000432
433# Here's the third example po file, used to generate MMO_DATA
434
435'''
436msgid ""
437msgstr ""
438"Project-Id-Version: No Project 0.0\n"
439"POT-Creation-Date: Wed Dec 11 07:44:15 2002\n"
440"PO-Revision-Date: 2002-08-14 01:18:58+00:00\n"
441"Last-Translator: John Doe <jdoe@example.com>\n"
442"Jane Foobar <jfoobar@example.com>\n"
443"Language-Team: xx <xx@example.com>\n"
444"MIME-Version: 1.0\n"
445"Content-Type: text/plain; charset=iso-8859-15\n"
446"Content-Transfer-Encoding: quoted-printable\n"
447"Generated-By: pygettext.py 1.3\n"
448'''