Merged release21-maint changes.
[python/dscho.git] / Lib / test / test_gettext.py
blob56a8524adcd480760484bb6724290f02975f4a75
1 import os
2 import base64
3 import gettext
6 def test_api_1(localedir, mofile):
7 print 'test api 1'
9 # Test basic interface
10 os.environ['LANGUAGE'] = 'xx'
12 print 'installing gettext'
13 gettext.install('gettext', localedir)
15 # test some translations
16 print _('albatross')
17 print _(u'mullusk')
18 print _(r'Raymond Luxury Yach-t')
19 print _(ur'nudge nudge')
21 # double quotes
22 print _("albatross")
23 print _(u"mullusk")
24 print _(r"Raymond Luxury Yach-t")
25 print _(ur"nudge nudge")
27 # triple single quotes
28 print _('''albatross''')
29 print _(u'''mullusk''')
30 print _(r'''Raymond Luxury Yach-t''')
31 print _(ur'''nudge nudge''')
33 # triple double quotes
34 print _("""albatross""")
35 print _(u"""mullusk""")
36 print _(r"""Raymond Luxury Yach-t""")
37 print _(ur"""nudge nudge""")
39 # multiline strings
40 print _('''This module provides internationalization and localization
41 support for your Python programs by providing an interface to the GNU
42 gettext message catalog library.''')
44 # test the alternative interface
45 fp = open(os.path.join(mofile), 'rb')
46 t = gettext.GNUTranslations(fp)
47 fp.close()
49 t.install()
51 print _('nudge nudge')
53 # try unicode return type
54 t.install(unicode=1)
56 print _('mullusk')
60 def test_api_2(localedir, mofile):
61 print 'test api 2'
63 gettext.bindtextdomain('gettext', localedir)
64 print gettext.bindtextdomain('gettext') == localedir
66 gettext.textdomain('gettext')
67 # should return 'gettext'
68 print gettext.textdomain()
70 # local function override builtin
71 _ = gettext.gettext
73 # test some translations
74 print _('albatross')
75 print _(u'mullusk')
76 print _(r'Raymond Luxury Yach-t')
77 print _(ur'nudge nudge')
79 # double quotes
80 print _("albatross")
81 print _(u"mullusk")
82 print _(r"Raymond Luxury Yach-t")
83 print _(ur"nudge nudge")
85 # triple single quotes
86 print _('''albatross''')
87 print _(u'''mullusk''')
88 print _(r'''Raymond Luxury Yach-t''')
89 print _(ur'''nudge nudge''')
91 # triple double quotes
92 print _("""albatross""")
93 print _(u"""mullusk""")
94 print _(r"""Raymond Luxury Yach-t""")
95 print _(ur"""nudge nudge""")
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.''')
102 # Now test dgettext()
103 def _(message):
104 return gettext.dgettext('gettext')
108 GNU_MO_DATA = '''\
109 3hIElQAAAAAFAAAAHAAAAEQAAAAHAAAAbAAAAAAAAACIAAAAFQAAAIkAAAChAAAAnwAAAAcAAABB
110 AQAACwAAAEkBAAAbAQAAVQEAABYAAABxAgAAoQAAAIgCAAAFAAAAKgMAAAkAAAAwAwAAAQAAAAQA
111 AAACAAAAAAAAAAUAAAAAAAAAAwAAAABSYXltb25kIEx1eHVyeSBZYWNoLXQAVGhpcyBtb2R1bGUg
112 cHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24gYW5kIGxvY2FsaXphdGlvbgpzdXBwb3J0IGZv
113 ciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92aWRpbmcgYW4gaW50ZXJmYWNlIHRvIHRoZSBH
114 TlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGlicmFyeS4AbXVsbHVzawBudWRnZSBudWRnZQBQ
115 cm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1SZXZpc2lvbi1EYXRlOiAyMDAwLTA4LTI5IDEyOjE5
116 LTA0OjAwCkxhc3QtVHJhbnNsYXRvcjogQmFycnkgQS4gV2Fyc2F3IDxid2Fyc2F3QGJlb3Blbi5j
117 b20+Ckxhbmd1YWdlLVRlYW06IFhYIDxweXRob24tZGV2QHB5dGhvbi5vcmc+Ck1JTUUtVmVyc2lv
118 bjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD1rb2k4X3IKQ29udGVudC1U
119 cmFuc2Zlci1FbmNvZGluZzogbm9uZQpHZW5lcmF0ZWQtQnk6IHB5Z2V0dGV4dC5weSAxLjEKAFRo
120 cm9hdHdvYmJsZXIgTWFuZ3JvdmUAR3V2ZiB6YnFoeXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZt
121 bmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVnIHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBv
122 bCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255
123 YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
127 LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
128 MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
130 def setup():
131 os.makedirs(LOCALEDIR)
132 fp = open(MOFILE, 'wb')
133 fp.write(base64.decodestring(GNU_MO_DATA))
134 fp.close()
136 def teardown():
137 os.unlink(MOFILE)
138 os.removedirs(LOCALEDIR)
141 try:
142 setup()
143 test_api_1(os.curdir, MOFILE)
144 test_api_2(os.curdir, MOFILE)
145 finally:
146 teardown()
147 pass
151 # For reference, here's the .po file used to created the .mo data above.
154 # Dummy translation for Python's test_gettext.py module.
155 # Copyright (C) 2000 BeOpen.com
156 # Barry Warsaw <bwarsaw@beopen.com>, 2000.
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"
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"
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 ""
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"
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"
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."