|
1 | 1 | import os |
| 2 | +import base64 |
2 | 3 | import gettext |
3 | 4 |
|
4 | | -def get_qualified_path(name): |
5 | | - """Return a more qualified path to name""" |
6 | | - import sys |
7 | | - import os |
8 | | - path = sys.path |
9 | | - try: |
10 | | - path = [os.path.dirname(__file__)] + path |
11 | | - except NameError: |
12 | | - pass |
13 | | - for dir in path: |
14 | | - fullname = os.path.join(dir, name) |
15 | | - if os.path.exists(fullname): |
16 | | - return fullname |
17 | | - return name |
18 | | - |
19 | | -# Test basic interface |
20 | | -os.environ['LANGUAGE'] = 'xx' |
21 | | - |
22 | | -mofile = get_qualified_path('xx') |
23 | | -localedir = os.path.dirname(mofile) |
24 | | - |
25 | | -print 'installing gettext' |
26 | | -gettext.install() |
27 | | - |
28 | | -print _('calling bindtextdomain with localedir %s') % localedir |
29 | | -print gettext.bindtextdomain('gettext', localedir) |
30 | | -print gettext.bindtextdomain() |
31 | | - |
32 | | -print gettext.textdomain('gettext') |
33 | | -print gettext.textdomain() |
34 | | - |
35 | | -# test some translations |
36 | | -print _(u'mullusk') |
37 | | -print _(r'Raymond Luxury Yach-t') |
38 | | -print _(ur'nudge nudge') |
39 | | - |
40 | | -# double quotes |
41 | | -print _(u"mullusk") |
42 | | -print _(r"Raymond Luxury Yach-t") |
43 | | -print _(ur"nudge nudge") |
44 | | - |
45 | | -# triple single quotes |
46 | | -print _(u'''mullusk''') |
47 | | -print _(r'''Raymond Luxury Yach-t''') |
48 | | -print _(ur'''nudge nudge''') |
49 | | - |
50 | | -# triple double quotes |
51 | | -print _(u"""mullusk""") |
52 | | -print _(r"""Raymond Luxury Yach-t""") |
53 | | -print _(ur"""nudge nudge""") |
54 | | - |
55 | | -# multiline strings |
56 | | -print _('''This module provides internationalization and localization |
| 5 | +def test(localedir, mofile): |
| 6 | + # Test basic interface |
| 7 | + os.environ['LANGUAGE'] = 'xx' |
| 8 | + |
| 9 | + print 'installing gettext' |
| 10 | + gettext.install() |
| 11 | + |
| 12 | + print _('calling bindtextdomain with localedir %s') % localedir |
| 13 | + print gettext.bindtextdomain('gettext', localedir) |
| 14 | + print gettext.bindtextdomain() |
| 15 | + |
| 16 | + print gettext.textdomain('gettext') |
| 17 | + print gettext.textdomain() |
| 18 | + |
| 19 | + # test some translations |
| 20 | + print _(u'mullusk') |
| 21 | + print _(r'Raymond Luxury Yach-t') |
| 22 | + print _(ur'nudge nudge') |
| 23 | + |
| 24 | + # double quotes |
| 25 | + print _(u"mullusk") |
| 26 | + print _(r"Raymond Luxury Yach-t") |
| 27 | + print _(ur"nudge nudge") |
| 28 | + |
| 29 | + # triple single quotes |
| 30 | + print _(u'''mullusk''') |
| 31 | + print _(r'''Raymond Luxury Yach-t''') |
| 32 | + print _(ur'''nudge nudge''') |
| 33 | + |
| 34 | + # triple double quotes |
| 35 | + print _(u"""mullusk""") |
| 36 | + print _(r"""Raymond Luxury Yach-t""") |
| 37 | + print _(ur"""nudge nudge""") |
| 38 | + |
| 39 | + # multiline strings |
| 40 | + print _('''This module provides internationalization and localization |
57 | 41 | support for your Python programs by providing an interface to the GNU |
58 | 42 | gettext message catalog library.''') |
59 | 43 |
|
60 | | -print gettext.dgettext('gettext', 'nudge nudge') |
| 44 | + print gettext.dgettext('gettext', 'nudge nudge') |
| 45 | + |
| 46 | + # dcgettext |
| 47 | + ##import locale |
| 48 | + ##if gettext.dcgettext('gettext', 'nudge nudge', |
| 49 | + ## locale.LC_MESSAGES) <> 'wink wink': |
| 50 | + ## print _('dcgettext failed') |
| 51 | + |
| 52 | + # test the alternative interface |
| 53 | + fp = open(os.path.join(mofile), 'rb') |
| 54 | + t = gettext.GNUTranslations(fp) |
| 55 | + fp.close() |
| 56 | + |
| 57 | + gettext.set(t) |
| 58 | + print t == gettext.get() |
| 59 | + |
| 60 | + print _('nudge nudge') |
| 61 | + |
| 62 | +GNU_MO_DATA = '''\ |
| 63 | +3hIElQAAAAAFAAAAHAAAAEQAAAAHAAAAbAAAAAAAAACIAAAAFQAAAIkAAAChAAAAnwAAAAc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ciBnYiBndXIgVEFICnRyZ2dya2cgenJmZm50ciBwbmdueWJ0IHl2 |
| 77 | +b2VuZWwuAGJhY29uAHdpbmsgd2luawA= |
| 78 | +''' |
| 79 | + |
| 80 | +SOLARIS_MO_DATA = '''\ |
| 81 | +AAAACAAAABEAAAIXAAAB0gAAARD///+d////nQAAAAAAAAAAAAAAAAAAAAIAAAAkAAAAAf///53/ |
| 82 | +//+dAAAAQgAAAAIAAAABAAAABQAAAGgAAAAD////nf///50AAAB0AAAADQAAAAQAAAAGAAAAmAAA |
| 83 | +AA7///+dAAAABwAAAKAAAAAU////nf///50AAAC5AAAAFQAAAAMAAAAMAAAAywAAABb///+d//// |
| 84 | +nQAAANsAAAAXAAAACQAAAAsAAADsAAAAGP///53///+dAAAA/wAAABkAAAAKAAAADgAAAScAAAAa |
| 85 | +////nf///50AAAFDAAAAGwAAAA0AAAAPAAABXgAAABz///+dAAAAEAAAAgAAAAC+////nf///50A |
| 86 | +AAIWAAAA1XRleHQgZG9tYWluIGRpZG4ndCBnZXQgc2V0IHByb3Blcmx5AHJhdyBzdHJpbmcgdHJh |
| 87 | +bnNsYXRpb24gZmFpbGVkAHJhdyBVbmljb2RlIHN0cmluZyB0cmFuc2xhdGlvbiBmYWlsZWQAbnVk |
| 88 | +Z2UgbnVkZ2UAbXVsdGlsaW5lIHN0cmluZyB0cmFuc2xhdGlvbiBmYWlsZWQAbXVsbHVzawBnb3Qg |
| 89 | +dW5leHBlY3RlZCBsb2NhbGVkaXIAZ2V0dGV4dCBpbnN0YWxsZWQAZGdldHRleHQgZmFpbGVkAGRj |
| 90 | +Z2V0dGV4dCBmYWlsZWQAY2FsbGluZyB0ZXh0ZG9tYWluAGNhbGxpbmcgYmluZHRleHRkb21haW4g |
| 91 | +d2l0aCBsb2NhbGRpciAlcwBiaW5kdGV4dGRvbWFpbihOb25lKSBmYWlsZWQAVW5pY29kZSB0cmFu |
| 92 | +c2xhdGlvbiBmYWlsZWQAVGhpcyBtb2R1bGUgcHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24g |
| 93 | +YW5kIGxvY2FsaXphdGlvbgpzdXBwb3J0IGZvciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92 |
| 94 | +aWRpbmcgYW4gaW50ZXJmYWNlIHRvIHRoZSBHTlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGli |
| 95 | +cmFyeS4AUmF5bW9uZCBMdXh1cnkgWWFjaC10AAAAAAB3aW5rIHdpbmsAAGJhY29uAAAAAAAAAAAA |
| 96 | +R3V2ZiB6YnFoeXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3Zi |
| 97 | +YQpmaGNjYmVnIHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVz |
| 98 | +bnByIGdiIGd1ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AVGhyb2F0d29i |
| 99 | +YmxlciBNYW5ncm92ZQBQcm9qZWN0LUlkLVZlcnNpb246IFBBQ0tBR0UgVkVSU0lPTgpQTy1SZXZp |
| 100 | +c2lvbi1EYXRlOiAyMDAwLTA4LTE4IDIxOjAxLTA0OjAwCkxhc3QtVHJhbnNsYXRvcjogRlVMTCBO |
| 101 | +QU1FIDxFTUFJTEBBRERSRVNTPgpMYW5ndWFnZS1UZWFtOiBMQU5HVUFHRSA8TExAbGkub3JnPgpN |
| 102 | +SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9Q0hBUlNF |
| 103 | +VApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBFTkNPRElORwoA |
| 104 | +''' |
| 105 | + |
| 106 | +LOCALEDIR = os.path.join('xx', 'LC_MESSAGES') |
| 107 | +MOFILE = os.path.join(LOCALEDIR, 'gettext.mo') |
| 108 | + |
| 109 | +def setup(): |
| 110 | + os.makedirs(LOCALEDIR) |
| 111 | + fp = open(MOFILE, 'w') |
| 112 | + fp.write(base64.decodestring(GNU_MO_DATA)) |
| 113 | + fp.close() |
| 114 | + |
| 115 | +def teardown(): |
| 116 | + os.unlink(MOFILE) |
| 117 | + os.removedirs(LOCALEDIR) |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +try: |
| 122 | + setup() |
| 123 | + test('.', MOFILE) |
| 124 | +finally: |
| 125 | + teardown() |
| 126 | + pass |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +# For reference, here's the .pot and .po files used to created the .mo data |
| 131 | +# above. The .pot file was generated by pygettext. |
| 132 | + |
| 133 | +# =============================== messages.pot |
| 134 | + |
| 135 | +### SOME DESCRIPTIVE TITLE. |
| 136 | +### Copyright (C) YEAR ORGANIZATION |
| 137 | +### FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
| 138 | +### |
| 139 | +##msgid "" |
| 140 | +##msgstr "" |
| 141 | +##"Project-Id-Version: PACKAGE VERSION\n" |
| 142 | +##"PO-Revision-Date: Fri Aug 18 20:45:03 2000\n" |
| 143 | +##"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 144 | +##"Language-Team: LANGUAGE <[email protected]>\n" |
| 145 | +##"MIME-Version: 1.0\n" |
| 146 | +##"Content-Type: text/plain; charset=CHARSET\n" |
| 147 | +##"Content-Transfer-Encoding: ENCODING\n" |
| 148 | +##"Generated-By: pygettext.py 1.1\n" |
| 149 | + |
| 150 | + |
| 151 | +###: Lib/test/test_gettext.py:34 |
| 152 | +##msgid "calling textdomain" |
| 153 | +##msgstr "" |
| 154 | + |
| 155 | +###: Lib/test/test_gettext.py:94 |
| 156 | +##msgid "dgettext failed" |
| 157 | +##msgstr "" |
| 158 | + |
| 159 | +###: Lib/test/test_gettext.py:25 |
| 160 | +##msgid "gettext installed" |
| 161 | +##msgstr "" |
| 162 | + |
| 163 | +###: Lib/test/test_gettext.py:32 |
| 164 | +##msgid "bindtextdomain(None) failed" |
| 165 | +##msgstr "" |
| 166 | + |
| 167 | +###: Lib/test/test_gettext.py:44 Lib/test/test_gettext.py:54 |
| 168 | +###: Lib/test/test_gettext.py:64 Lib/test/test_gettext.py:74 |
| 169 | +##msgid "Raymond Luxury Yach-t" |
| 170 | +##msgstr "" |
| 171 | + |
| 172 | +###: Lib/test/test_gettext.py:41 Lib/test/test_gettext.py:51 |
| 173 | +###: Lib/test/test_gettext.py:61 Lib/test/test_gettext.py:71 |
| 174 | +##msgid "mullusk" |
| 175 | +##msgstr "" |
| 176 | + |
| 177 | +###: Lib/test/test_gettext.py:38 |
| 178 | +##msgid "text domain didn't get set properly" |
| 179 | +##msgstr "" |
| 180 | + |
| 181 | +###: Lib/test/test_gettext.py:98 |
| 182 | +##msgid "dcgettext failed" |
| 183 | +##msgstr "" |
| 184 | + |
| 185 | +###: Lib/test/test_gettext.py:47 Lib/test/test_gettext.py:57 |
| 186 | +###: Lib/test/test_gettext.py:67 Lib/test/test_gettext.py:77 |
| 187 | +##msgid "nudge nudge" |
| 188 | +##msgstr "" |
| 189 | + |
| 190 | +###: Lib/test/test_gettext.py:45 Lib/test/test_gettext.py:55 |
| 191 | +###: Lib/test/test_gettext.py:65 Lib/test/test_gettext.py:75 |
| 192 | +##msgid "raw string translation failed" |
| 193 | +##msgstr "" |
| 194 | + |
| 195 | +###: Lib/test/test_gettext.py:30 |
| 196 | +##msgid "got unexpected localedir" |
| 197 | +##msgstr "" |
| 198 | + |
| 199 | +###: Lib/test/test_gettext.py:48 Lib/test/test_gettext.py:58 |
| 200 | +###: Lib/test/test_gettext.py:68 Lib/test/test_gettext.py:78 |
| 201 | +##msgid "raw Unicode string translation failed" |
| 202 | +##msgstr "" |
| 203 | + |
| 204 | +###: Lib/test/test_gettext.py:42 Lib/test/test_gettext.py:52 |
| 205 | +###: Lib/test/test_gettext.py:62 Lib/test/test_gettext.py:72 |
| 206 | +##msgid "Unicode translation failed" |
| 207 | +##msgstr "" |
| 208 | + |
| 209 | +###: Lib/test/test_gettext.py:90 |
| 210 | +##msgid "multiline string translation failed" |
| 211 | +##msgstr "" |
| 212 | + |
| 213 | +###: Lib/test/test_gettext.py:81 |
| 214 | +##msgid "" |
| 215 | +##"This module provides internationalization and localization\n" |
| 216 | +##"support for your Python programs by providing an interface to the GNU\n" |
| 217 | +##"gettext message catalog library." |
| 218 | +##msgstr "" |
| 219 | + |
| 220 | +###: Lib/test/test_gettext.py:26 |
| 221 | +##msgid "calling bindtextdomain with localdir %s" |
| 222 | +##msgstr "" |
| 223 | + |
| 224 | +# =============================== messages.po |
| 225 | + |
| 226 | +### Dummy translation for Python's test_gettext.py module. |
| 227 | +### Copyright (C) 2000 BeOpen.com |
| 228 | +### Barry Warsaw <[email protected]>, 2000. |
| 229 | +### |
| 230 | +###, fuzzy |
| 231 | +##msgid "" |
| 232 | +##msgstr "" |
| 233 | +##"Project-Id-Version: PACKAGE VERSION\n" |
| 234 | +##"PO-Revision-Date: 2000-08-18 21:01-04:00\n" |
| 235 | +##"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 236 | +##"Language-Team: LANGUAGE <[email protected]>\n" |
| 237 | +##"MIME-Version: 1.0\n" |
| 238 | +##"Content-Type: text/plain; charset=CHARSET\n" |
| 239 | +##"Content-Transfer-Encoding: ENCODING\n" |
| 240 | + |
| 241 | +### SOME DESCRIPTIVE TITLE. |
| 242 | +### Copyright (C) YEAR ORGANIZATION |
| 243 | +### FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
| 244 | +### |
| 245 | +##msgid "" |
| 246 | +##msgstr "" |
| 247 | +##"Project-Id-Version: 2.0\n" |
| 248 | +##"PO-Revision-Date: Fri Aug 18 20:45:03 2000\n" |
| 249 | +##"Last-Translator: Barry Warsaw <[email protected]>\n" |
| 250 | +##"Language-Team: XX <[email protected]>\n" |
| 251 | +##"MIME-Version: 1.0\n" |
| 252 | +##"Content-Type: text/plain; charset=en\n" |
| 253 | +##"Content-Transfer-Encoding: none\n" |
| 254 | +##"Generated-By: pygettext.py 1.1\n" |
| 255 | + |
| 256 | + |
| 257 | +###: Lib/test/test_gettext.py:34 |
| 258 | +##msgid "calling textdomain" |
| 259 | +##msgstr "" |
| 260 | + |
| 261 | +###: Lib/test/test_gettext.py:94 |
| 262 | +##msgid "dgettext failed" |
| 263 | +##msgstr "" |
| 264 | + |
| 265 | +###: Lib/test/test_gettext.py:25 |
| 266 | +##msgid "gettext installed" |
| 267 | +##msgstr "" |
| 268 | + |
| 269 | +###: Lib/test/test_gettext.py:32 |
| 270 | +##msgid "bindtextdomain(None) failed" |
| 271 | +##msgstr "" |
| 272 | + |
| 273 | +###: Lib/test/test_gettext.py:44 Lib/test/test_gettext.py:54 |
| 274 | +###: Lib/test/test_gettext.py:64 Lib/test/test_gettext.py:74 |
| 275 | +##msgid "Raymond Luxury Yach-t" |
| 276 | +##msgstr "Throatwobbler Mangrove" |
| 277 | + |
| 278 | +###: Lib/test/test_gettext.py:41 Lib/test/test_gettext.py:51 |
| 279 | +###: Lib/test/test_gettext.py:61 Lib/test/test_gettext.py:71 |
| 280 | +##msgid "mullusk" |
| 281 | +##msgstr "bacon" |
| 282 | + |
| 283 | +###: Lib/test/test_gettext.py:38 |
| 284 | +##msgid "text domain didn't get set properly" |
| 285 | +##msgstr "" |
| 286 | + |
| 287 | +###: Lib/test/test_gettext.py:98 |
| 288 | +##msgid "dcgettext failed" |
| 289 | +##msgstr "" |
| 290 | + |
| 291 | +###: Lib/test/test_gettext.py:47 Lib/test/test_gettext.py:57 |
| 292 | +###: Lib/test/test_gettext.py:67 Lib/test/test_gettext.py:77 |
| 293 | +##msgid "nudge nudge" |
| 294 | +##msgstr "wink wink" |
| 295 | + |
| 296 | +###: Lib/test/test_gettext.py:45 Lib/test/test_gettext.py:55 |
| 297 | +###: Lib/test/test_gettext.py:65 Lib/test/test_gettext.py:75 |
| 298 | +##msgid "raw string translation failed" |
| 299 | +##msgstr "" |
| 300 | + |
| 301 | +###: Lib/test/test_gettext.py:30 |
| 302 | +##msgid "got unexpected localedir" |
| 303 | +##msgstr "" |
| 304 | + |
| 305 | +###: Lib/test/test_gettext.py:48 Lib/test/test_gettext.py:58 |
| 306 | +###: Lib/test/test_gettext.py:68 Lib/test/test_gettext.py:78 |
| 307 | +##msgid "raw Unicode string translation failed" |
| 308 | +##msgstr "" |
61 | 309 |
|
62 | | -# dcgettext |
63 | | -##import locale |
64 | | -##if gettext.dcgettext('gettext', 'nudge nudge', |
65 | | -## locale.LC_MESSAGES) <> 'wink wink': |
66 | | -## print _('dcgettext failed') |
| 310 | +###: Lib/test/test_gettext.py:42 Lib/test/test_gettext.py:52 |
| 311 | +###: Lib/test/test_gettext.py:62 Lib/test/test_gettext.py:72 |
| 312 | +##msgid "Unicode translation failed" |
| 313 | +##msgstr "" |
67 | 314 |
|
68 | | -# test the alternative interface |
69 | | -fp = open(os.path.join(mofile, 'LC_MESSAGES', 'gettext.mo'), 'rb') |
70 | | -t = gettext.GNUTranslations(fp) |
71 | | -fp.close() |
| 315 | +###: Lib/test/test_gettext.py:90 |
| 316 | +##msgid "multiline string translation failed" |
| 317 | +##msgstr "" |
72 | 318 |
|
73 | | -gettext.set(t) |
74 | | -print t == gettext.get() |
| 319 | +###: Lib/test/test_gettext.py:81 |
| 320 | +##msgid "" |
| 321 | +##"This module provides internationalization and localization\n" |
| 322 | +##"support for your Python programs by providing an interface to the GNU\n" |
| 323 | +##"gettext message catalog library." |
| 324 | +##msgstr "Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n" |
| 325 | +##"fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n" |
| 326 | +##"trggrkg zrffntr pngnybt yvoenel." |
75 | 327 |
|
76 | | -print _('nudge nudge') |
| 328 | +###: Lib/test/test_gettext.py:26 |
| 329 | +##msgid "calling bindtextdomain with localdir %s" |
| 330 | +##msgstr "" |
0 commit comments