blob: ddad86b5c454afad7308e879bd415a4dfdbaca59 [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
10 os.environ['LANGUAGE'] = 'xx'
Barry Warsaw9e035922000-08-25 19:14:25 +000011
Barry Warsawff6d8132000-08-25 19:50:38 +000012 print 'installing gettext'
Barry Warsaw64dab462000-08-30 03:32:07 +000013 gettext.install('gettext', localedir)
Barry Warsaw9e035922000-08-25 19:14:25 +000014
Barry Warsawff6d8132000-08-25 19:50:38 +000015 # test some translations
Barry Warsaw64dab462000-08-30 03:32:07 +000016 print _('albatross')
Barry Warsawff6d8132000-08-25 19:50:38 +000017 print _(u'mullusk')
18 print _(r'Raymond Luxury Yach-t')
19 print _(ur'nudge nudge')
Barry Warsaw9e035922000-08-25 19:14:25 +000020
Barry Warsawff6d8132000-08-25 19:50:38 +000021 # double quotes
Barry Warsaw64dab462000-08-30 03:32:07 +000022 print _("albatross")
Barry Warsawff6d8132000-08-25 19:50:38 +000023 print _(u"mullusk")
24 print _(r"Raymond Luxury Yach-t")
25 print _(ur"nudge nudge")
Barry Warsaw9e035922000-08-25 19:14:25 +000026
Barry Warsawff6d8132000-08-25 19:50:38 +000027 # triple single quotes
Barry Warsaw64dab462000-08-30 03:32:07 +000028 print _('''albatross''')
Barry Warsawff6d8132000-08-25 19:50:38 +000029 print _(u'''mullusk''')
30 print _(r'''Raymond Luxury Yach-t''')
31 print _(ur'''nudge nudge''')
Barry Warsaw9e035922000-08-25 19:14:25 +000032
Barry Warsawff6d8132000-08-25 19:50:38 +000033 # triple double quotes
Barry Warsaw64dab462000-08-30 03:32:07 +000034 print _("""albatross""")
Barry Warsawff6d8132000-08-25 19:50:38 +000035 print _(u"""mullusk""")
36 print _(r"""Raymond Luxury Yach-t""")
37 print _(ur"""nudge nudge""")
Barry Warsaw9e035922000-08-25 19:14:25 +000038
Barry Warsawff6d8132000-08-25 19:50:38 +000039 # multiline strings
40 print _('''This module provides internationalization and localization
Barry Warsaw9e035922000-08-25 19:14:25 +000041support for your Python programs by providing an interface to the GNU
42gettext message catalog library.''')
43
Barry Warsawff6d8132000-08-25 19:50:38 +000044 # test the alternative interface
45 fp = open(os.path.join(mofile), 'rb')
46 t = gettext.GNUTranslations(fp)
47 fp.close()
Barry Warsaw9e035922000-08-25 19:14:25 +000048
Barry Warsaw64dab462000-08-30 03:32:07 +000049 t.install()
Barry Warsaw9e035922000-08-25 19:14:25 +000050
Barry Warsawff6d8132000-08-25 19:50:38 +000051 print _('nudge nudge')
52
Barry Warsaw64dab462000-08-30 03:32:07 +000053 # try unicode return type
54 t.install(unicode=1)
55
56 print _('mullusk')
57
58
Fred Drake004d5e62000-10-23 17:22:08 +000059
Barry Warsaw64dab462000-08-30 03:32:07 +000060def 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
99support for your Python programs by providing an interface to the GNU
100gettext message catalog library.''')
101
102 # Now test dgettext()
103 def _(message):
104 return gettext.dgettext('gettext')
105
106
Fred Drake004d5e62000-10-23 17:22:08 +0000107
Barry Warsawff6d8132000-08-25 19:50:38 +0000108GNU_MO_DATA = '''\
1093hIElQAAAAAFAAAAHAAAAEQAAAAHAAAAbAAAAAAAAACIAAAAFQAAAIkAAAChAAAAnwAAAAcAAABB
Barry Warsaw08f99562001-08-28 21:26:33 +0000110AQAACwAAAEkBAAAZAQAAVQEAABYAAABvAgAAoQAAAIYCAAAFAAAAKAMAAAkAAAAuAwAAAQAAAAQA
Barry Warsawff6d8132000-08-25 19:50:38 +0000111AAACAAAAAAAAAAUAAAAAAAAAAwAAAABSYXltb25kIEx1eHVyeSBZYWNoLXQAVGhpcyBtb2R1bGUg
112cHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24gYW5kIGxvY2FsaXphdGlvbgpzdXBwb3J0IGZv
113ciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92aWRpbmcgYW4gaW50ZXJmYWNlIHRvIHRoZSBH
114TlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGlicmFyeS4AbXVsbHVzawBudWRnZSBudWRnZQBQ
Barry Warsaw64dab462000-08-30 03:32:07 +0000115cm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1SZXZpc2lvbi1EYXRlOiAyMDAwLTA4LTI5IDEyOjE5
Barry Warsaw08f99562001-08-28 21:26:33 +0000116LTA0OjAwCkxhc3QtVHJhbnNsYXRvcjogQmFycnkgQS4gV2Fyc2F3IDxiYXJyeUBweXRob24ub3Jn
117PgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246
118IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9a29pOF9yCkNvbnRlbnQtVHJh
119bnNmZXItRW5jb2Rpbmc6IG5vbmUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4xCgBUaHJv
120YXR3b2JibGVyIE1hbmdyb3ZlAEd1dmYgemJxaHlyIGNlYml2cXJmIHZhZ3JlYW5ndmJhbnl2bW5n
121dmJhIG5hcSB5YnBueXZtbmd2YmEKZmhjY2JlZyBzYmUgbGJoZSBDbGd1YmEgY2VidGVuemYgb2wg
122Y2ViaXZxdmF0IG5hIHZhZ3Jlc25wciBnYiBndXIgVEFICnRyZ2dya2cgenJmZm50ciBwbmdueWJ0
123IHl2b2VuZWwuAGJhY29uAHdpbmsgd2luawA=
Barry Warsawff6d8132000-08-25 19:50:38 +0000124'''
125
Fred Drake004d5e62000-10-23 17:22:08 +0000126
Barry Warsawff6d8132000-08-25 19:50:38 +0000127LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
128MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
129
130def setup():
131 os.makedirs(LOCALEDIR)
Tim Petersc7951952000-08-26 21:01:27 +0000132 fp = open(MOFILE, 'wb')
Barry Warsawff6d8132000-08-25 19:50:38 +0000133 fp.write(base64.decodestring(GNU_MO_DATA))
134 fp.close()
135
136def teardown():
137 os.unlink(MOFILE)
138 os.removedirs(LOCALEDIR)
139
140
Barry Warsawff6d8132000-08-25 19:50:38 +0000141try:
142 setup()
Jack Jansen8b7c3c02000-09-15 12:58:08 +0000143 test_api_1(os.curdir, MOFILE)
144 test_api_2(os.curdir, MOFILE)
Barry Warsawff6d8132000-08-25 19:50:38 +0000145finally:
146 teardown()
147 pass
148
149
Fred Drake004d5e62000-10-23 17:22:08 +0000150
Barry Warsaw64dab462000-08-30 03:32:07 +0000151# For reference, here's the .po file used to created the .mo data above.
Barry Warsawff6d8132000-08-25 19:50:38 +0000152
Barry Warsaw64dab462000-08-30 03:32:07 +0000153'''
154# Dummy translation for Python's test_gettext.py module.
Barry Warsaw19148732001-08-24 19:11:57 +0000155# Copyright (C) 2001 Python Software Foundation
156# Barry Warsaw <barry@python.org>, 2000.
Barry Warsaw64dab462000-08-30 03:32:07 +0000157#
158msgid ""
159msgstr ""
160"Project-Id-Version: 2.0\n"
161"PO-Revision-Date: 2000-08-29 12:19-04:00\n"
Barry Warsaw08f99562001-08-28 21:26:33 +0000162"Last-Translator: Barry A. Warsaw <barry@python.org>\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000163"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 Warsawff6d8132000-08-25 19:50:38 +0000168
Barry Warsaw64dab462000-08-30 03:32:07 +0000169#: 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
172msgid "nudge nudge"
173msgstr "wink wink"
Barry Warsawff6d8132000-08-25 19:50:38 +0000174
Barry Warsaw64dab462000-08-30 03:32:07 +0000175#: 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
177msgid "albatross"
178msgstr ""
Barry Warsawff6d8132000-08-25 19:50:38 +0000179
Barry Warsaw64dab462000-08-30 03:32:07 +0000180#: 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
182msgid "Raymond Luxury Yach-t"
183msgstr "Throatwobbler Mangrove"
Barry Warsawff6d8132000-08-25 19:50:38 +0000184
Barry Warsaw64dab462000-08-30 03:32:07 +0000185#: 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
188msgid "mullusk"
189msgstr "bacon"
Barry Warsawff6d8132000-08-25 19:50:38 +0000190
Barry Warsaw64dab462000-08-30 03:32:07 +0000191#: test_gettext.py:40 test_gettext.py:101
192msgid ""
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."
196msgstr ""
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'''