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 | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 3 | import gettext |
| 4 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 5 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 6 | def test_api_1(localedir, mofile): |
| 7 | print 'test api 1' |
| 8 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 9 | # Test basic interface |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 10 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 11 | print 'installing gettext' |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 12 | gettext.install('gettext', localedir) |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 13 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 14 | # test some translations |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 15 | print _('albatross') |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 16 | print _(u'mullusk') |
| 17 | print _(r'Raymond Luxury Yach-t') |
| 18 | print _(ur'nudge nudge') |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 19 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 20 | # double quotes |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 21 | print _("albatross") |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 22 | print _(u"mullusk") |
| 23 | print _(r"Raymond Luxury Yach-t") |
| 24 | print _(ur"nudge nudge") |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 25 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 26 | # triple single quotes |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 27 | print _('''albatross''') |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 28 | print _(u'''mullusk''') |
| 29 | print _(r'''Raymond Luxury Yach-t''') |
| 30 | print _(ur'''nudge nudge''') |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 31 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 32 | # triple double quotes |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 33 | print _("""albatross""") |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 34 | print _(u"""mullusk""") |
| 35 | print _(r"""Raymond Luxury Yach-t""") |
| 36 | print _(ur"""nudge nudge""") |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 37 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 38 | # multiline strings |
| 39 | print _('''This module provides internationalization and localization |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 40 | support for your Python programs by providing an interface to the GNU |
| 41 | gettext message catalog library.''') |
| 42 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 43 | # test the alternative interface |
| 44 | fp = open(os.path.join(mofile), 'rb') |
| 45 | t = gettext.GNUTranslations(fp) |
| 46 | fp.close() |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 47 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 48 | t.install() |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 49 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 50 | print _('nudge nudge') |
| 51 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 52 | # try unicode return type |
| 53 | t.install(unicode=1) |
| 54 | |
| 55 | print _('mullusk') |
| 56 | |
| 57 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 58 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 59 | def test_api_2(localedir, mofile): |
| 60 | print 'test api 2' |
| 61 | |
| 62 | gettext.bindtextdomain('gettext', localedir) |
| 63 | print gettext.bindtextdomain('gettext') == localedir |
| 64 | |
| 65 | gettext.textdomain('gettext') |
| 66 | # should return 'gettext' |
| 67 | print gettext.textdomain() |
| 68 | |
| 69 | # local function override builtin |
| 70 | _ = gettext.gettext |
| 71 | |
| 72 | # test some translations |
| 73 | print _('albatross') |
| 74 | print _(u'mullusk') |
| 75 | print _(r'Raymond Luxury Yach-t') |
| 76 | print _(ur'nudge nudge') |
| 77 | |
| 78 | # double quotes |
| 79 | print _("albatross") |
| 80 | print _(u"mullusk") |
| 81 | print _(r"Raymond Luxury Yach-t") |
| 82 | print _(ur"nudge nudge") |
| 83 | |
| 84 | # triple single quotes |
| 85 | print _('''albatross''') |
| 86 | print _(u'''mullusk''') |
| 87 | print _(r'''Raymond Luxury Yach-t''') |
| 88 | print _(ur'''nudge nudge''') |
| 89 | |
| 90 | # triple double quotes |
| 91 | print _("""albatross""") |
| 92 | print _(u"""mullusk""") |
| 93 | print _(r"""Raymond Luxury Yach-t""") |
| 94 | print _(ur"""nudge nudge""") |
| 95 | |
| 96 | # multiline strings |
| 97 | print _('''This module provides internationalization and localization |
| 98 | support for your Python programs by providing an interface to the GNU |
| 99 | gettext message catalog library.''') |
| 100 | |
| 101 | # Now test dgettext() |
| 102 | def _(message): |
| 103 | return gettext.dgettext('gettext') |
| 104 | |
| 105 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 106 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 107 | GNU_MO_DATA = '''\ |
| 108 | 3hIElQAAAAAFAAAAHAAAAEQAAAAHAAAAbAAAAAAAAACIAAAAFQAAAIkAAAChAAAAnwAAAAcAAABB |
Barry Warsaw | 08f9956 | 2001-08-28 21:26:33 +0000 | [diff] [blame] | 109 | AQAACwAAAEkBAAAZAQAAVQEAABYAAABvAgAAoQAAAIYCAAAFAAAAKAMAAAkAAAAuAwAAAQAAAAQA |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 110 | AAACAAAAAAAAAAUAAAAAAAAAAwAAAABSYXltb25kIEx1eHVyeSBZYWNoLXQAVGhpcyBtb2R1bGUg |
| 111 | cHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24gYW5kIGxvY2FsaXphdGlvbgpzdXBwb3J0IGZv |
| 112 | ciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92aWRpbmcgYW4gaW50ZXJmYWNlIHRvIHRoZSBH |
| 113 | TlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGlicmFyeS4AbXVsbHVzawBudWRnZSBudWRnZQBQ |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 114 | cm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1SZXZpc2lvbi1EYXRlOiAyMDAwLTA4LTI5IDEyOjE5 |
Barry Warsaw | 08f9956 | 2001-08-28 21:26:33 +0000 | [diff] [blame] | 115 | LTA0OjAwCkxhc3QtVHJhbnNsYXRvcjogQmFycnkgQS4gV2Fyc2F3IDxiYXJyeUBweXRob24ub3Jn |
| 116 | PgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246 |
| 117 | IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9a29pOF9yCkNvbnRlbnQtVHJh |
| 118 | bnNmZXItRW5jb2Rpbmc6IG5vbmUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4xCgBUaHJv |
| 119 | YXR3b2JibGVyIE1hbmdyb3ZlAEd1dmYgemJxaHlyIGNlYml2cXJmIHZhZ3JlYW5ndmJhbnl2bW5n |
| 120 | dmJhIG5hcSB5YnBueXZtbmd2YmEKZmhjY2JlZyBzYmUgbGJoZSBDbGd1YmEgY2VidGVuemYgb2wg |
| 121 | Y2ViaXZxdmF0IG5hIHZhZ3Jlc25wciBnYiBndXIgVEFICnRyZ2dya2cgenJmZm50ciBwbmdueWJ0 |
| 122 | IHl2b2VuZWwuAGJhY29uAHdpbmsgd2luawA= |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 123 | ''' |
| 124 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 125 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 126 | LOCALEDIR = os.path.join('xx', 'LC_MESSAGES') |
| 127 | MOFILE = os.path.join(LOCALEDIR, 'gettext.mo') |
| 128 | |
| 129 | def setup(): |
| 130 | os.makedirs(LOCALEDIR) |
Tim Peters | c795195 | 2000-08-26 21:01:27 +0000 | [diff] [blame] | 131 | fp = open(MOFILE, 'wb') |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 132 | fp.write(base64.decodestring(GNU_MO_DATA)) |
| 133 | fp.close() |
Guido van Rossum | 6569257 | 2002-07-20 00:36:38 +0000 | [diff] [blame^] | 134 | os.environ['LANGUAGE'] = 'xx' |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 135 | |
| 136 | def teardown(): |
Guido van Rossum | 6569257 | 2002-07-20 00:36:38 +0000 | [diff] [blame^] | 137 | os.environ['LANGUAGE'] = 'en' |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 138 | os.unlink(MOFILE) |
| 139 | os.removedirs(LOCALEDIR) |
| 140 | |
| 141 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 142 | try: |
| 143 | setup() |
Jack Jansen | 8b7c3c0 | 2000-09-15 12:58:08 +0000 | [diff] [blame] | 144 | test_api_1(os.curdir, MOFILE) |
| 145 | test_api_2(os.curdir, MOFILE) |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 146 | finally: |
| 147 | teardown() |
| 148 | pass |
| 149 | |
| 150 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 151 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 152 | # For reference, here's the .po file used to created the .mo data above. |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 153 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 154 | ''' |
| 155 | # Dummy translation for Python's test_gettext.py module. |
Barry Warsaw | 1914873 | 2001-08-24 19:11:57 +0000 | [diff] [blame] | 156 | # Copyright (C) 2001 Python Software Foundation |
| 157 | # Barry Warsaw <barry@python.org>, 2000. |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 158 | # |
| 159 | msgid "" |
| 160 | msgstr "" |
| 161 | "Project-Id-Version: 2.0\n" |
| 162 | "PO-Revision-Date: 2000-08-29 12:19-04:00\n" |
Barry Warsaw | 08f9956 | 2001-08-28 21:26:33 +0000 | [diff] [blame] | 163 | "Last-Translator: Barry A. Warsaw <barry@python.org>\n" |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 164 | "Language-Team: XX <python-dev@python.org>\n" |
| 165 | "MIME-Version: 1.0\n" |
| 166 | "Content-Type: text/plain; charset=koi8_r\n" |
| 167 | "Content-Transfer-Encoding: none\n" |
| 168 | "Generated-By: pygettext.py 1.1\n" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 169 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 170 | #: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37 |
| 171 | #: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92 |
| 172 | #: test_gettext.py:98 |
| 173 | msgid "nudge nudge" |
| 174 | msgstr "wink wink" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 175 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 176 | #: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34 |
| 177 | #: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95 |
| 178 | msgid "albatross" |
| 179 | msgstr "" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 180 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 181 | #: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36 |
| 182 | #: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97 |
| 183 | msgid "Raymond Luxury Yach-t" |
| 184 | msgstr "Throatwobbler Mangrove" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 185 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 186 | #: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35 |
| 187 | #: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90 |
| 188 | #: test_gettext.py:96 |
| 189 | msgid "mullusk" |
| 190 | msgstr "bacon" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 191 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 192 | #: test_gettext.py:40 test_gettext.py:101 |
| 193 | msgid "" |
| 194 | "This module provides internationalization and localization\n" |
| 195 | "support for your Python programs by providing an interface to the GNU\n" |
| 196 | "gettext message catalog library." |
| 197 | msgstr "" |
| 198 | "Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n" |
| 199 | "fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n" |
| 200 | "trggrkg zrffntr pngnybt yvoenel." |
| 201 | ''' |