blob: 5456948ca4b0488cc92c0d50dbebf673794eff56 [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import 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')
Barry Warsawff6d8132000-08-25 19:50:38 +000061
62
Barry Warsawa1ce93f2003-04-11 18:36:43 +000063class GettextBaseTest(unittest.TestCase):
Martin v. Löwisd8996052002-11-21 21:45:32 +000064 def setUp(self):
Neal Norwitza0e0cd32004-07-19 00:08:59 +000065 if not os.path.isdir(LOCALEDIR):
66 os.makedirs(LOCALEDIR)
Éric Araujo7fa886d2010-11-06 04:11:59 +000067 with open(MOFILE, 'wb') as fp:
68 fp.write(base64.decodebytes(GNU_MO_DATA))
69 with open(UMOFILE, 'wb') as fp:
70 fp.write(base64.decodebytes(UMO_DATA))
71 with open(MMOFILE, 'wb') as fp:
72 fp.write(base64.decodebytes(MMO_DATA))
Walter Dörwald155374d2009-05-01 19:58:58 +000073 self.env = support.EnvironmentVarGuard()
74 self.env['LANGUAGE'] = 'xx'
Éric Araujo37a89332010-11-06 04:09:29 +000075 gettext._translations.clear()
Barry Warsawa1ce93f2003-04-11 18:36:43 +000076
77 def tearDown(self):
Walter Dörwald155374d2009-05-01 19:58:58 +000078 self.env.__exit__()
79 del self.env
Brett Cannon4aebbb02003-04-27 19:42:41 +000080 shutil.rmtree(os.path.split(LOCALEDIR)[0])
Barry Warsawa1ce93f2003-04-11 18:36:43 +000081
82
83class GettextTestCase1(GettextBaseTest):
84 def setUp(self):
85 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +000086 self.localedir = os.curdir
87 self.mofile = MOFILE
Martin v. Löwisd8996052002-11-21 21:45:32 +000088 gettext.install('gettext', self.localedir)
89
Martin v. Löwisd8996052002-11-21 21:45:32 +000090 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +000091 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +000092 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +000093 eq(_('albatross'), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +000094 eq(_('mullusk'), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +000095 eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +000096 eq(_(r'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +000097
98 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +000099 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000100 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000101 eq(_("albatross"), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000102 eq(_("mullusk"), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000103 eq(_(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000104 eq(_(r"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000105
106 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000107 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000108 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000109 eq(_('''albatross'''), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000110 eq(_('''mullusk'''), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000111 eq(_(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000112 eq(_(r'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000113
114 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000115 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000116 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000117 eq(_("""albatross"""), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000118 eq(_("""mullusk"""), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000119 eq(_(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000120 eq(_(r"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000121
122 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000123 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000124 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000125 eq(_('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000126support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000127gettext message catalog library.'''),
128 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000129fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000130trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000131
132 def test_the_alternative_interface(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000133 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000134 # test the alternative interface
Éric Araujo7fa886d2010-11-06 04:11:59 +0000135 with open(self.mofile, 'rb') as fp:
136 t = gettext.GNUTranslations(fp)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000137 # Install the translation object
Martin v. Löwisd8996052002-11-21 21:45:32 +0000138 t.install()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000139 eq(_('nudge nudge'), 'wink wink')
140 # Try unicode return type
Benjamin Peterson801844d2008-07-14 14:32:15 +0000141 t.install()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000142 eq(_('mullusk'), 'bacon')
Georg Brandl602b9ba2006-02-19 13:26:36 +0000143 # Test installation of other methods
Georg Brandl1a3284e2007-12-02 09:40:06 +0000144 import builtins
Benjamin Peterson801844d2008-07-14 14:32:15 +0000145 t.install(names=["gettext", "lgettext"])
146 eq(_, t.gettext)
147 eq(builtins.gettext, t.gettext)
Georg Brandl602b9ba2006-02-19 13:26:36 +0000148 eq(lgettext, t.lgettext)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000149 del builtins.gettext
150 del builtins.lgettext
Martin v. Löwisd8996052002-11-21 21:45:32 +0000151
152
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000153class GettextTestCase2(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000154 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000155 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000156 self.localedir = os.curdir
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000157 # Set up the bindings
Martin v. Löwisd8996052002-11-21 21:45:32 +0000158 gettext.bindtextdomain('gettext', self.localedir)
159 gettext.textdomain('gettext')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000160 # For convenience
Martin v. Löwisd8996052002-11-21 21:45:32 +0000161 self._ = gettext.gettext
162
Martin v. Löwisd8996052002-11-21 21:45:32 +0000163 def test_bindtextdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000164 self.assertEqual(gettext.bindtextdomain('gettext'), self.localedir)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000165
166 def test_textdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000167 self.assertEqual(gettext.textdomain(), 'gettext')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000168
169 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000170 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000171 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000172 eq(self._('albatross'), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000173 eq(self._('mullusk'), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000174 eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000175 eq(self._(r'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000176
177 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000178 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000179 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000180 eq(self._("albatross"), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000181 eq(self._("mullusk"), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000182 eq(self._(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000183 eq(self._(r"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000184
185 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000186 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000187 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000188 eq(self._('''albatross'''), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000189 eq(self._('''mullusk'''), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000190 eq(self._(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000191 eq(self._(r'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000192
193 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000194 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000195 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000196 eq(self._("""albatross"""), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000197 eq(self._("""mullusk"""), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000198 eq(self._(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000199 eq(self._(r"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000200
201 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000202 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000203 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000204 eq(self._('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000205support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000206gettext message catalog library.'''),
207 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000208fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000209trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000210
211
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000212class PluralFormsTestCase(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000213 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000214 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000215 self.mofile = MOFILE
216
217 def test_plural_forms1(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000218 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000219 x = gettext.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000220 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000221 x = gettext.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000222 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000223
224 def test_plural_forms2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000225 eq = self.assertEqual
Éric Araujo7fa886d2010-11-06 04:11:59 +0000226 with open(self.mofile, 'rb') as fp:
227 t = gettext.GNUTranslations(fp)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000228 x = t.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000229 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000230 x = t.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000231 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000232
233 def test_hu(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000234 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000235 f = gettext.c2py('0')
236 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000237 eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000238
239 def test_de(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('n != 1')
242 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000243 eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000244
245 def test_fr(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, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000250
251 def test_gd(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 ? 0 : n==2 ? 1 : 2')
254 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000255 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000256
257 def test_gd2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000258 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000259 # Tests the combination of parentheses and "?:"
260 f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
261 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000262 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000263
264 def test_lt(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000265 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000266 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
267 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000268 eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000269
270 def test_ru(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%10<=4 && (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, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000275
276 def test_pl(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==1 ? 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, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000281
282 def test_sl(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%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
285 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000286 eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000287
288 def test_security(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000289 raises = self.assertRaises
Martin v. Löwisd8996052002-11-21 21:45:32 +0000290 # Test for a dangerous expression
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000291 raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000292
293
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000294class UnicodeTranslationsTest(GettextBaseTest):
295 def setUp(self):
296 GettextBaseTest.setUp(self)
Éric Araujo7fa886d2010-11-06 04:11:59 +0000297 with open(UMOFILE, 'rb') as fp:
Barry Warsawedb155f2003-04-24 18:08:13 +0000298 self.t = gettext.GNUTranslations(fp)
Benjamin Peterson801844d2008-07-14 14:32:15 +0000299 self._ = self.t.gettext
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000300
301 def test_unicode_msgid(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000302 unless = self.assertTrue
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000303 unless(isinstance(self._(''), str))
304 unless(isinstance(self._(''), str))
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000305
306 def test_unicode_msgstr(self):
307 eq = self.assertEqual
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000308 eq(self._('ab\xde'), '\xa4yz')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000309
310
Barry Warsawe960e222003-05-20 17:28:54 +0000311class WeirdMetadataTest(GettextBaseTest):
312 def setUp(self):
313 GettextBaseTest.setUp(self)
Éric Araujo7fa886d2010-11-06 04:11:59 +0000314 with open(MMOFILE, 'rb') as fp:
Barry Warsawe960e222003-05-20 17:28:54 +0000315 try:
316 self.t = gettext.GNUTranslations(fp)
317 except:
318 self.tearDown()
319 raise
Barry Warsawe960e222003-05-20 17:28:54 +0000320
321 def test_weird_metadata(self):
322 info = self.t.info()
Christian Heimes6ae5d7f2007-10-31 18:53:44 +0000323 self.assertEqual(len(info), 9)
Barry Warsawe960e222003-05-20 17:28:54 +0000324 self.assertEqual(info['last-translator'],
325 'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
326
327
Éric Araujo6108bf52010-10-04 23:52:37 +0000328class DummyGNUTranslations(gettext.GNUTranslations):
329 def foo(self):
330 return 'foo'
331
332
333class GettextCacheTestCase(GettextBaseTest):
334 def test_cache(self):
335 self.localedir = os.curdir
336 self.mofile = MOFILE
337
338 self.assertEqual(len(gettext._translations), 0)
339
340 t = gettext.translation('gettext', self.localedir)
341
342 self.assertEqual(len(gettext._translations), 1)
343
344 t = gettext.translation('gettext', self.localedir,
345 class_=DummyGNUTranslations)
346
347 self.assertEqual(len(gettext._translations), 2)
348 self.assertEqual(t.__class__, DummyGNUTranslations)
349
350 # Calling it again doesn't add to the cache
351
352 t = gettext.translation('gettext', self.localedir,
353 class_=DummyGNUTranslations)
354
355 self.assertEqual(len(gettext._translations), 2)
356 self.assertEqual(t.__class__, DummyGNUTranslations)
357
358
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000359def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000360 support.run_unittest(__name__)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000361
362if __name__ == '__main__':
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000363 test_main()
Martin v. Löwisd8996052002-11-21 21:45:32 +0000364
Barry Warsawff6d8132000-08-25 19:50:38 +0000365
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000366# For reference, here's the .po file used to created the GNU_MO_DATA above.
Martin v. Löwisd8996052002-11-21 21:45:32 +0000367#
368# The original version was automatically generated from the sources with
369# pygettext. Later it was manually modified to add plural forms support.
Barry Warsawff6d8132000-08-25 19:50:38 +0000370
Barry Warsaw64dab462000-08-30 03:32:07 +0000371'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000372# Dummy translation for the Python test_gettext.py module.
Barry Warsaw19148732001-08-24 19:11:57 +0000373# Copyright (C) 2001 Python Software Foundation
374# Barry Warsaw <barry@python.org>, 2000.
Barry Warsaw64dab462000-08-30 03:32:07 +0000375#
376msgid ""
377msgstr ""
378"Project-Id-Version: 2.0\n"
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000379"PO-Revision-Date: 2003-04-11 14:32-0400\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000380"Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000381"Language-Team: XX <python-dev@python.org>\n"
382"MIME-Version: 1.0\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000383"Content-Type: text/plain; charset=iso-8859-1\n"
384"Content-Transfer-Encoding: 8bit\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000385"Generated-By: pygettext.py 1.1\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000386"Plural-Forms: nplurals=2; plural=n!=1;\n"
Barry Warsawff6d8132000-08-25 19:50:38 +0000387
Barry Warsaw64dab462000-08-30 03:32:07 +0000388#: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
389#: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
390#: test_gettext.py:98
391msgid "nudge nudge"
392msgstr "wink wink"
Barry Warsawff6d8132000-08-25 19:50:38 +0000393
Barry Warsaw64dab462000-08-30 03:32:07 +0000394#: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
395#: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
396msgid "albatross"
397msgstr ""
Barry Warsawff6d8132000-08-25 19:50:38 +0000398
Barry Warsaw64dab462000-08-30 03:32:07 +0000399#: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
400#: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
401msgid "Raymond Luxury Yach-t"
402msgstr "Throatwobbler Mangrove"
Barry Warsawff6d8132000-08-25 19:50:38 +0000403
Barry Warsaw64dab462000-08-30 03:32:07 +0000404#: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
405#: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
406#: test_gettext.py:96
407msgid "mullusk"
408msgstr "bacon"
Barry Warsawff6d8132000-08-25 19:50:38 +0000409
Barry Warsaw64dab462000-08-30 03:32:07 +0000410#: test_gettext.py:40 test_gettext.py:101
411msgid ""
412"This module provides internationalization and localization\n"
413"support for your Python programs by providing an interface to the GNU\n"
414"gettext message catalog library."
415msgstr ""
416"Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
417"fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
418"trggrkg zrffntr pngnybt yvoenel."
Martin v. Löwisd8996052002-11-21 21:45:32 +0000419
420# Manually added, as neither pygettext nor xgettext support plural forms
421# in Python.
422msgid "There is %s file"
423msgid_plural "There are %s files"
424msgstr[0] "Hay %s fichero"
425msgstr[1] "Hay %s ficheros"
Barry Warsaw64dab462000-08-30 03:32:07 +0000426'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000427
428# Here's the second example po file example, used to generate the UMO_DATA
429# containing utf-8 encoded Unicode strings
430
431'''
432# Dummy translation for the Python test_gettext.py module.
433# Copyright (C) 2001 Python Software Foundation
434# Barry Warsaw <barry@python.org>, 2000.
435#
436msgid ""
437msgstr ""
438"Project-Id-Version: 2.0\n"
439"PO-Revision-Date: 2003-04-11 12:42-0400\n"
440"Last-Translator: Barry A. WArsaw <barry@python.org>\n"
441"Language-Team: XX <python-dev@python.org>\n"
442"MIME-Version: 1.0\n"
443"Content-Type: text/plain; charset=utf-8\n"
444"Content-Transfer-Encoding: 7bit\n"
445"Generated-By: manually\n"
446
447#: nofile:0
448msgid "ab\xc3\x9e"
449msgstr "\xc2\xa4yz"
450'''
Barry Warsawe960e222003-05-20 17:28:54 +0000451
452# Here's the third example po file, used to generate MMO_DATA
453
454'''
455msgid ""
456msgstr ""
457"Project-Id-Version: No Project 0.0\n"
458"POT-Creation-Date: Wed Dec 11 07:44:15 2002\n"
459"PO-Revision-Date: 2002-08-14 01:18:58+00:00\n"
460"Last-Translator: John Doe <jdoe@example.com>\n"
461"Jane Foobar <jfoobar@example.com>\n"
462"Language-Team: xx <xx@example.com>\n"
463"MIME-Version: 1.0\n"
464"Content-Type: text/plain; charset=iso-8859-15\n"
465"Content-Transfer-Encoding: quoted-printable\n"
466"Generated-By: pygettext.py 1.3\n"
467'''