Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 1 | import os |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 2 | import base64 |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 3 | import shutil |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 4 | import gettext |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 5 | import unittest |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 6 | |
| 7 | from test.test_support import run_suite |
| 8 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 9 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 10 | # 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 Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 15 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 16 | GNU_MO_DATA = '''\ |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 17 | 3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj |
| 18 | AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD |
| 19 | AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh |
| 20 | eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU |
| 21 | aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u |
| 22 | CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh |
| 23 | Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51 |
| 24 | ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt |
| 25 | MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k |
| 26 | YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN |
| 27 | SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4 |
| 28 | NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0 |
| 29 | ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0 |
| 30 | d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo |
| 31 | eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn |
| 32 | IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1 |
| 33 | ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA== |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 34 | ''' |
| 35 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 36 | UMO_DATA = '''\ |
| 37 | 3hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABAAAAFEAAAAPAQAAVgAAAAQAAABm |
| 38 | AQAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAYWLDngBQcm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1S |
| 39 | ZXZpc2lvbi1EYXRlOiAyMDAzLTA0LTExIDEyOjQyLTA0MDAKTGFzdC1UcmFuc2xhdG9yOiBCYXJy |
| 40 | eSBBLiBXQXJzYXcgPGJhcnJ5QHB5dGhvbi5vcmc+Ckxhbmd1YWdlLVRlYW06IFhYIDxweXRob24t |
| 41 | ZGV2QHB5dGhvbi5vcmc+Ck1JTUUtVmVyc2lvbjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFp |
| 42 | bjsgY2hhcnNldD11dGYtOApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0CkdlbmVyYXRl |
| 43 | ZC1CeTogbWFudWFsbHkKAMKkeXoA |
| 44 | ''' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 45 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 46 | LOCALEDIR = os.path.join('xx', 'LC_MESSAGES') |
| 47 | MOFILE = os.path.join(LOCALEDIR, 'gettext.mo') |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 48 | UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo') |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 49 | |
| 50 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 51 | class GettextBaseTest(unittest.TestCase): |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 52 | def setUp(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 53 | os.makedirs(LOCALEDIR) |
| 54 | fp = open(MOFILE, 'wb') |
| 55 | fp.write(base64.decodestring(GNU_MO_DATA)) |
| 56 | fp.close() |
| 57 | fp = open(UMOFILE, 'wb') |
| 58 | fp.write(base64.decodestring(UMO_DATA)) |
| 59 | fp.close() |
| 60 | os.environ['LANGUAGE'] = 'xx' |
| 61 | |
| 62 | def tearDown(self): |
| 63 | os.environ['LANGUAGE'] = 'en' |
| 64 | shutil.rmtree(LOCALEDIR) |
| 65 | |
| 66 | |
| 67 | class GettextTestCase1(GettextBaseTest): |
| 68 | def setUp(self): |
| 69 | GettextBaseTest.setUp(self) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 70 | self.localedir = os.curdir |
| 71 | self.mofile = MOFILE |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 72 | gettext.install('gettext', self.localedir) |
| 73 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 74 | def test_some_translations(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 75 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 76 | # test some translations |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 77 | eq(_('albatross'), 'albatross') |
| 78 | eq(_(u'mullusk'), 'bacon') |
| 79 | eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove') |
| 80 | eq(_(ur'nudge nudge'), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 81 | |
| 82 | def test_double_quotes(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 83 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 84 | # double quotes |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 85 | eq(_("albatross"), 'albatross') |
| 86 | eq(_(u"mullusk"), 'bacon') |
| 87 | eq(_(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove') |
| 88 | eq(_(ur"nudge nudge"), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 89 | |
| 90 | def test_triple_single_quotes(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 91 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 92 | # triple single quotes |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 93 | eq(_('''albatross'''), 'albatross') |
| 94 | eq(_(u'''mullusk'''), 'bacon') |
| 95 | eq(_(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove') |
| 96 | eq(_(ur'''nudge nudge'''), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 97 | |
| 98 | def test_triple_double_quotes(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 99 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 100 | # triple double quotes |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 101 | eq(_("""albatross"""), 'albatross') |
| 102 | eq(_(u"""mullusk"""), 'bacon') |
| 103 | eq(_(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove') |
| 104 | eq(_(ur"""nudge nudge"""), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 105 | |
| 106 | def test_multiline_strings(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 107 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 108 | # multiline strings |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 109 | eq(_('''This module provides internationalization and localization |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 110 | support for your Python programs by providing an interface to the GNU |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 111 | gettext message catalog library.'''), |
| 112 | '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 113 | fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 114 | trggrkg zrffntr pngnybt yvoenel.''') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 115 | |
| 116 | def test_the_alternative_interface(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 117 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 118 | # test the alternative interface |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 119 | fp = open(self.mofile, 'rb') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 120 | t = gettext.GNUTranslations(fp) |
| 121 | fp.close() |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 122 | # Install the translation object |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 123 | t.install() |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 124 | eq(_('nudge nudge'), 'wink wink') |
| 125 | # Try unicode return type |
| 126 | t.install(unicode=True) |
| 127 | eq(_('mullusk'), 'bacon') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 128 | |
| 129 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 130 | class GettextTestCase2(GettextBaseTest): |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 131 | def setUp(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 132 | GettextBaseTest.setUp(self) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 133 | self.localedir = os.curdir |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 134 | # Set up the bindings |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 135 | gettext.bindtextdomain('gettext', self.localedir) |
| 136 | gettext.textdomain('gettext') |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 137 | # For convenience |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 138 | self._ = gettext.gettext |
| 139 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 140 | def test_bindtextdomain(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 141 | self.assertEqual(gettext.bindtextdomain('gettext'), self.localedir) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 142 | |
| 143 | def test_textdomain(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 144 | self.assertEqual(gettext.textdomain(), 'gettext') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 145 | |
| 146 | def test_some_translations(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 147 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 148 | # test some translations |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 149 | eq(self._('albatross'), 'albatross') |
| 150 | eq(self._(u'mullusk'), 'bacon') |
| 151 | eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove') |
| 152 | eq(self._(ur'nudge nudge'), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 153 | |
| 154 | def test_double_quotes(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 155 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 156 | # double quotes |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 157 | eq(self._("albatross"), 'albatross') |
| 158 | eq(self._(u"mullusk"), 'bacon') |
| 159 | eq(self._(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove') |
| 160 | eq(self._(ur"nudge nudge"), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 161 | |
| 162 | def test_triple_single_quotes(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 163 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 164 | # triple single quotes |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 165 | eq(self._('''albatross'''), 'albatross') |
| 166 | eq(self._(u'''mullusk'''), 'bacon') |
| 167 | eq(self._(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove') |
| 168 | eq(self._(ur'''nudge nudge'''), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 169 | |
| 170 | def test_triple_double_quotes(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 171 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 172 | # triple double quotes |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 173 | eq(self._("""albatross"""), 'albatross') |
| 174 | eq(self._(u"""mullusk"""), 'bacon') |
| 175 | eq(self._(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove') |
| 176 | eq(self._(ur"""nudge nudge"""), 'wink wink') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 177 | |
| 178 | def test_multiline_strings(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 179 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 180 | # multiline strings |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 181 | eq(self._('''This module provides internationalization and localization |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 182 | support for your Python programs by providing an interface to the GNU |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 183 | gettext message catalog library.'''), |
| 184 | '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 185 | fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 186 | trggrkg zrffntr pngnybt yvoenel.''') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 187 | |
| 188 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 189 | class PluralFormsTestCase(GettextBaseTest): |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 190 | def setUp(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 191 | GettextBaseTest.setUp(self) |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 192 | self.mofile = MOFILE |
| 193 | |
| 194 | def test_plural_forms1(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 195 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 196 | x = gettext.ngettext('There is %s file', 'There are %s files', 1) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 197 | eq(x, 'Hay %s fichero') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 198 | x = gettext.ngettext('There is %s file', 'There are %s files', 2) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 199 | eq(x, 'Hay %s ficheros') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 200 | |
| 201 | def test_plural_forms2(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 202 | eq = self.assertEqual |
| 203 | fp = open(self.mofile, 'rb') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 204 | t = gettext.GNUTranslations(fp) |
| 205 | fp.close() |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 206 | x = t.ngettext('There is %s file', 'There are %s files', 1) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 207 | eq(x, 'Hay %s fichero') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 208 | x = t.ngettext('There is %s file', 'There are %s files', 2) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 209 | eq(x, 'Hay %s ficheros') |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 210 | |
| 211 | def test_hu(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 212 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 213 | f = gettext.c2py('0') |
| 214 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 215 | eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 216 | |
| 217 | def test_de(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 218 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 219 | f = gettext.c2py('n != 1') |
| 220 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 221 | eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 222 | |
| 223 | def test_fr(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 224 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 225 | f = gettext.c2py('n>1') |
| 226 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 227 | eq(s, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 228 | |
| 229 | def test_gd(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 230 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 231 | f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2') |
| 232 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 233 | eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 234 | |
| 235 | def test_gd2(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 236 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 237 | # Tests the combination of parentheses and "?:" |
| 238 | f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)') |
| 239 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 240 | eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 241 | |
| 242 | def test_lt(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 243 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 244 | f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2') |
| 245 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 246 | eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 247 | |
| 248 | def test_ru(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 249 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 250 | f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2') |
| 251 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 252 | eq(s, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 253 | |
| 254 | def test_pl(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 255 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 256 | f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2') |
| 257 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 258 | eq(s, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 259 | |
| 260 | def test_sl(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 261 | eq = self.assertEqual |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 262 | f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3') |
| 263 | s = ''.join([ str(f(x)) for x in range(200) ]) |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 264 | eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 265 | |
| 266 | def test_security(self): |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 267 | raises = self.assertRaises |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 268 | # Test for a dangerous expression |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 269 | raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)") |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 270 | |
| 271 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 272 | class UnicodeTranslationsTest(GettextBaseTest): |
| 273 | def setUp(self): |
| 274 | GettextBaseTest.setUp(self) |
| 275 | fp = open(UMOFILE, 'rb') |
| 276 | try: |
| 277 | self.t = gettext.GNUTranslations(fp, coerce=True) |
| 278 | finally: |
| 279 | fp.close() |
| 280 | self._ = self.t.ugettext |
| 281 | |
| 282 | def test_unicode_msgid(self): |
| 283 | unless = self.failUnless |
| 284 | unless(isinstance(self._(''), unicode)) |
| 285 | unless(isinstance(self._(u''), unicode)) |
| 286 | |
| 287 | def test_unicode_msgstr(self): |
| 288 | eq = self.assertEqual |
| 289 | eq(self._(u'ab\xde'), u'\xa4yz') |
| 290 | |
| 291 | |
| 292 | def suite(): |
| 293 | suite = unittest.TestSuite() |
| 294 | suite.addTest(unittest.makeSuite(GettextTestCase1)) |
| 295 | suite.addTest(unittest.makeSuite(GettextTestCase2)) |
| 296 | suite.addTest(unittest.makeSuite(PluralFormsTestCase)) |
| 297 | suite.addTest(unittest.makeSuite(UnicodeTranslationsTest)) |
| 298 | return suite |
| 299 | |
| 300 | |
| 301 | def test_main(): |
| 302 | run_suite(suite()) |
| 303 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 304 | |
| 305 | if __name__ == '__main__': |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 306 | test_main() |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 307 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 308 | |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 309 | # For reference, here's the .po file used to created the GNU_MO_DATA above. |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 310 | # |
| 311 | # The original version was automatically generated from the sources with |
| 312 | # pygettext. Later it was manually modified to add plural forms support. |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 313 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 314 | ''' |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 315 | # Dummy translation for the Python test_gettext.py module. |
Barry Warsaw | 1914873 | 2001-08-24 19:11:57 +0000 | [diff] [blame] | 316 | # Copyright (C) 2001 Python Software Foundation |
| 317 | # Barry Warsaw <barry@python.org>, 2000. |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 318 | # |
| 319 | msgid "" |
| 320 | msgstr "" |
| 321 | "Project-Id-Version: 2.0\n" |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 322 | "PO-Revision-Date: 2003-04-11 14:32-0400\n" |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 323 | "Last-Translator: J. David Ibanez <j-david@noos.fr>\n" |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 324 | "Language-Team: XX <python-dev@python.org>\n" |
| 325 | "MIME-Version: 1.0\n" |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 326 | "Content-Type: text/plain; charset=iso-8859-1\n" |
| 327 | "Content-Transfer-Encoding: 8bit\n" |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 328 | "Generated-By: pygettext.py 1.1\n" |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 329 | "Plural-Forms: nplurals=2; plural=n!=1;\n" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 330 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 331 | #: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37 |
| 332 | #: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92 |
| 333 | #: test_gettext.py:98 |
| 334 | msgid "nudge nudge" |
| 335 | msgstr "wink wink" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 336 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 337 | #: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34 |
| 338 | #: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95 |
| 339 | msgid "albatross" |
| 340 | msgstr "" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 341 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 342 | #: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36 |
| 343 | #: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97 |
| 344 | msgid "Raymond Luxury Yach-t" |
| 345 | msgstr "Throatwobbler Mangrove" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 346 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 347 | #: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35 |
| 348 | #: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90 |
| 349 | #: test_gettext.py:96 |
| 350 | msgid "mullusk" |
| 351 | msgstr "bacon" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 352 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 353 | #: test_gettext.py:40 test_gettext.py:101 |
| 354 | msgid "" |
| 355 | "This module provides internationalization and localization\n" |
| 356 | "support for your Python programs by providing an interface to the GNU\n" |
| 357 | "gettext message catalog library." |
| 358 | msgstr "" |
| 359 | "Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n" |
| 360 | "fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n" |
| 361 | "trggrkg zrffntr pngnybt yvoenel." |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 362 | |
| 363 | # Manually added, as neither pygettext nor xgettext support plural forms |
| 364 | # in Python. |
| 365 | msgid "There is %s file" |
| 366 | msgid_plural "There are %s files" |
| 367 | msgstr[0] "Hay %s fichero" |
| 368 | msgstr[1] "Hay %s ficheros" |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 369 | ''' |
Barry Warsaw | a1ce93f | 2003-04-11 18:36:43 +0000 | [diff] [blame] | 370 | |
| 371 | # Here's the second example po file example, used to generate the UMO_DATA |
| 372 | # containing utf-8 encoded Unicode strings |
| 373 | |
| 374 | ''' |
| 375 | # Dummy translation for the Python test_gettext.py module. |
| 376 | # Copyright (C) 2001 Python Software Foundation |
| 377 | # Barry Warsaw <barry@python.org>, 2000. |
| 378 | # |
| 379 | msgid "" |
| 380 | msgstr "" |
| 381 | "Project-Id-Version: 2.0\n" |
| 382 | "PO-Revision-Date: 2003-04-11 12:42-0400\n" |
| 383 | "Last-Translator: Barry A. WArsaw <barry@python.org>\n" |
| 384 | "Language-Team: XX <python-dev@python.org>\n" |
| 385 | "MIME-Version: 1.0\n" |
| 386 | "Content-Type: text/plain; charset=utf-8\n" |
| 387 | "Content-Transfer-Encoding: 7bit\n" |
| 388 | "Generated-By: manually\n" |
| 389 | |
| 390 | #: nofile:0 |
| 391 | msgid "ab\xc3\x9e" |
| 392 | msgstr "\xc2\xa4yz" |
| 393 | ''' |