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