blob: 08787d0636c74070989a4636cb78486e378ede2f [file] [log] [blame]
Barry Warsaw9e035922000-08-25 19:14:25 +00001import os
Barry Warsawff6d8132000-08-25 19:50:38 +00002import base64
Barry Warsaw9e035922000-08-25 19:14:25 +00003import gettext
4
Fred Drake004d5e62000-10-23 17:22:08 +00005
Barry Warsaw64dab462000-08-30 03:32:07 +00006def test_api_1(localedir, mofile):
7 print 'test api 1'
8
Barry Warsawff6d8132000-08-25 19:50:38 +00009 # Test basic interface
Barry Warsaw9e035922000-08-25 19:14:25 +000010
Barry Warsawff6d8132000-08-25 19:50:38 +000011 print 'installing gettext'
Barry Warsaw64dab462000-08-30 03:32:07 +000012 gettext.install('gettext', localedir)
Barry Warsaw9e035922000-08-25 19:14:25 +000013
Barry Warsawff6d8132000-08-25 19:50:38 +000014 # test some translations
Barry Warsaw64dab462000-08-30 03:32:07 +000015 print _('albatross')
Barry Warsawff6d8132000-08-25 19:50:38 +000016 print _(u'mullusk')
17 print _(r'Raymond Luxury Yach-t')
18 print _(ur'nudge nudge')
Barry Warsaw9e035922000-08-25 19:14:25 +000019
Barry Warsawff6d8132000-08-25 19:50:38 +000020 # double quotes
Barry Warsaw64dab462000-08-30 03:32:07 +000021 print _("albatross")
Barry Warsawff6d8132000-08-25 19:50:38 +000022 print _(u"mullusk")
23 print _(r"Raymond Luxury Yach-t")
24 print _(ur"nudge nudge")
Barry Warsaw9e035922000-08-25 19:14:25 +000025
Barry Warsawff6d8132000-08-25 19:50:38 +000026 # triple single quotes
Barry Warsaw64dab462000-08-30 03:32:07 +000027 print _('''albatross''')
Barry Warsawff6d8132000-08-25 19:50:38 +000028 print _(u'''mullusk''')
29 print _(r'''Raymond Luxury Yach-t''')
30 print _(ur'''nudge nudge''')
Barry Warsaw9e035922000-08-25 19:14:25 +000031
Barry Warsawff6d8132000-08-25 19:50:38 +000032 # triple double quotes
Barry Warsaw64dab462000-08-30 03:32:07 +000033 print _("""albatross""")
Barry Warsawff6d8132000-08-25 19:50:38 +000034 print _(u"""mullusk""")
35 print _(r"""Raymond Luxury Yach-t""")
36 print _(ur"""nudge nudge""")
Barry Warsaw9e035922000-08-25 19:14:25 +000037
Barry Warsawff6d8132000-08-25 19:50:38 +000038 # multiline strings
39 print _('''This module provides internationalization and localization
Barry Warsaw9e035922000-08-25 19:14:25 +000040support for your Python programs by providing an interface to the GNU
41gettext message catalog library.''')
42
Barry Warsawff6d8132000-08-25 19:50:38 +000043 # test the alternative interface
44 fp = open(os.path.join(mofile), 'rb')
45 t = gettext.GNUTranslations(fp)
46 fp.close()
Barry Warsaw9e035922000-08-25 19:14:25 +000047
Barry Warsaw64dab462000-08-30 03:32:07 +000048 t.install()
Barry Warsaw9e035922000-08-25 19:14:25 +000049
Barry Warsawff6d8132000-08-25 19:50:38 +000050 print _('nudge nudge')
51
Barry Warsaw64dab462000-08-30 03:32:07 +000052 # try unicode return type
53 t.install(unicode=1)
54
55 print _('mullusk')
56
57
Fred Drake004d5e62000-10-23 17:22:08 +000058
Barry Warsaw64dab462000-08-30 03:32:07 +000059def 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
98support for your Python programs by providing an interface to the GNU
99gettext message catalog library.''')
100
101 # Now test dgettext()
102 def _(message):
103 return gettext.dgettext('gettext')
104
105
Fred Drake004d5e62000-10-23 17:22:08 +0000106
Barry Warsawff6d8132000-08-25 19:50:38 +0000107GNU_MO_DATA = '''\
1083hIElQAAAAAFAAAAHAAAAEQAAAAHAAAAbAAAAAAAAACIAAAAFQAAAIkAAAChAAAAnwAAAAcAAABB
Barry Warsaw08f99562001-08-28 21:26:33 +0000109AQAACwAAAEkBAAAZAQAAVQEAABYAAABvAgAAoQAAAIYCAAAFAAAAKAMAAAkAAAAuAwAAAQAAAAQA
Barry Warsawff6d8132000-08-25 19:50:38 +0000110AAACAAAAAAAAAAUAAAAAAAAAAwAAAABSYXltb25kIEx1eHVyeSBZYWNoLXQAVGhpcyBtb2R1bGUg
111cHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24gYW5kIGxvY2FsaXphdGlvbgpzdXBwb3J0IGZv
112ciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92aWRpbmcgYW4gaW50ZXJmYWNlIHRvIHRoZSBH
113TlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGlicmFyeS4AbXVsbHVzawBudWRnZSBudWRnZQBQ
Barry Warsaw64dab462000-08-30 03:32:07 +0000114cm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1SZXZpc2lvbi1EYXRlOiAyMDAwLTA4LTI5IDEyOjE5
Barry Warsaw08f99562001-08-28 21:26:33 +0000115LTA0OjAwCkxhc3QtVHJhbnNsYXRvcjogQmFycnkgQS4gV2Fyc2F3IDxiYXJyeUBweXRob24ub3Jn
116PgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246
117IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9a29pOF9yCkNvbnRlbnQtVHJh
118bnNmZXItRW5jb2Rpbmc6IG5vbmUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4xCgBUaHJv
119YXR3b2JibGVyIE1hbmdyb3ZlAEd1dmYgemJxaHlyIGNlYml2cXJmIHZhZ3JlYW5ndmJhbnl2bW5n
120dmJhIG5hcSB5YnBueXZtbmd2YmEKZmhjY2JlZyBzYmUgbGJoZSBDbGd1YmEgY2VidGVuemYgb2wg
121Y2ViaXZxdmF0IG5hIHZhZ3Jlc25wciBnYiBndXIgVEFICnRyZ2dya2cgenJmZm50ciBwbmdueWJ0
122IHl2b2VuZWwuAGJhY29uAHdpbmsgd2luawA=
Barry Warsawff6d8132000-08-25 19:50:38 +0000123'''
124
Fred Drake004d5e62000-10-23 17:22:08 +0000125
Barry Warsawff6d8132000-08-25 19:50:38 +0000126LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
127MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
128
129def setup():
130 os.makedirs(LOCALEDIR)
Tim Petersc7951952000-08-26 21:01:27 +0000131 fp = open(MOFILE, 'wb')
Barry Warsawff6d8132000-08-25 19:50:38 +0000132 fp.write(base64.decodestring(GNU_MO_DATA))
133 fp.close()
Guido van Rossum65692572002-07-20 00:36:38 +0000134 os.environ['LANGUAGE'] = 'xx'
Barry Warsawff6d8132000-08-25 19:50:38 +0000135
136def teardown():
Guido van Rossum65692572002-07-20 00:36:38 +0000137 os.environ['LANGUAGE'] = 'en'
Barry Warsawff6d8132000-08-25 19:50:38 +0000138 os.unlink(MOFILE)
139 os.removedirs(LOCALEDIR)
140
141
Barry Warsawff6d8132000-08-25 19:50:38 +0000142try:
143 setup()
Jack Jansen8b7c3c02000-09-15 12:58:08 +0000144 test_api_1(os.curdir, MOFILE)
145 test_api_2(os.curdir, MOFILE)
Barry Warsawff6d8132000-08-25 19:50:38 +0000146finally:
147 teardown()
148 pass
149
150
Fred Drake004d5e62000-10-23 17:22:08 +0000151
Barry Warsaw64dab462000-08-30 03:32:07 +0000152# For reference, here's the .po file used to created the .mo data above.
Barry Warsawff6d8132000-08-25 19:50:38 +0000153
Barry Warsaw64dab462000-08-30 03:32:07 +0000154'''
155# Dummy translation for Python's test_gettext.py module.
Barry Warsaw19148732001-08-24 19:11:57 +0000156# Copyright (C) 2001 Python Software Foundation
157# Barry Warsaw <barry@python.org>, 2000.
Barry Warsaw64dab462000-08-30 03:32:07 +0000158#
159msgid ""
160msgstr ""
161"Project-Id-Version: 2.0\n"
162"PO-Revision-Date: 2000-08-29 12:19-04:00\n"
Barry Warsaw08f99562001-08-28 21:26:33 +0000163"Last-Translator: Barry A. Warsaw <barry@python.org>\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000164"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 Warsawff6d8132000-08-25 19:50:38 +0000169
Barry Warsaw64dab462000-08-30 03:32:07 +0000170#: 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
173msgid "nudge nudge"
174msgstr "wink wink"
Barry Warsawff6d8132000-08-25 19:50:38 +0000175
Barry Warsaw64dab462000-08-30 03:32:07 +0000176#: 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
178msgid "albatross"
179msgstr ""
Barry Warsawff6d8132000-08-25 19:50:38 +0000180
Barry Warsaw64dab462000-08-30 03:32:07 +0000181#: 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
183msgid "Raymond Luxury Yach-t"
184msgstr "Throatwobbler Mangrove"
Barry Warsawff6d8132000-08-25 19:50:38 +0000185
Barry Warsaw64dab462000-08-30 03:32:07 +0000186#: 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
189msgid "mullusk"
190msgstr "bacon"
Barry Warsawff6d8132000-08-25 19:50:38 +0000191
Barry Warsaw64dab462000-08-30 03:32:07 +0000192#: test_gettext.py:40 test_gettext.py:101
193msgid ""
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."
197msgstr ""
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'''